How to save image as Bytes field in cassandra database using cql engine?

I want to save images of my site in the cassandra database! I have to read and store bytes of the image. Do you have a good code for me? I am using python2.7, django framework and cql-engine!

This is my code:

1- My model:

from cqlengine import columns from cqlengine.models import Model class UserImage(Model): Email = columns.Text(primary_key=True) image=columns.Bytes(required=False) 

2- My form:

 class UserImage(forms.Form): image=forms.ImageField() 

3- My view:

 from MainAPP.models import UserImage as UserImageModel from MainAPP.forms import UsersForms from django.http import HttpResponse from cqlengine import connection from PIL import Image def UploadImage(request): if request.method == 'POST': form = UsersForms.UserImage(request.POST, request.FILES) if form.is_valid(): try: image_data=Image.open(request.FILES['image']) except IOError: return HttpResponse("cannot upload %s"% request.FILES['image'].name) connection.setup(['127.0.0.1:9160']) UserImageModel.create(Email='ebrahim i.omid69@gmail.com ', image=image_data) return HttpResponse('Stored Successfully!') else: form= UsersForms.UserImage() return render_to_response('Users/uploadImage.html', {'form': form}, context_instance=RequestContext(request)) 

My template:

 {% block content %} <form enctype="multipart/form-data" method="post" action=""> {% csrf_token %} {{ form.as_p }} <button type="submit">Upload</button> </form> {% endblock %} 

My error in the rendered template:

 AttributeError at /uploadImage encode Request Method: POST Request URL: http://127.0.0.1:8000/uploadImage Django Version: 1.5.1 Exception Type: AttributeError Exception Value: encode 

What do you think? Please help me...

+6
source share
2 answers

My altered view: We can store the image in cassandra without using PIL:

 def UploadImage(request): if request.method == 'POST': form = UsersForms.UserImage(request.POST, request.FILES) if form.is_valid(): try: image_data=request.FILES['image'].read() except IOError: return HttpResponse("cannot convert %s"% request.FILES['image'].name) connection.setup(['127.0.0.1:9160']) UserImageModel.create(Email=' ebrahimi.omid69@gmail.com ', image=image_data) return HttpResponse(request.FILES['image'].name) else: form= UsersForms.UserImage() return render_to_response('Users/uploadImage.html', {'form': form}, context_instance=RequestContext(request)) 
0
source

If you look at the exception message you receive, it says: AttributeError: encode. This tells you that somewhere in this code path, something is looking for an attribute (or, most likely, a method) called β€œencode” on some object and not detecting it.

I suspect that you are not passing the correct duck object for cqlengine to UserImage.image. Does he know how to speak PIL image objects? I doubt it. I'm sure cqlengine is looking for a typical python.encode string method ( http://docs.python.org/2/library/stdtypes.html#str.encode ). Instead of sending the PIL image, try transferring only the raw bytes that you get from posting the form.

+2
source

Source: https://habr.com/ru/post/949680/


All Articles