I want to store my website images in cassandra database! I must read and store bytes of image. Do you have a nice code for me? I'm 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='[email protected]', 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 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 is your idea? Please guide me...
If you look at the exception message you are getting, it says AttributeError: encode. This is telling you that somewhere in that code path, something is looking for an attribute (or most likely a method) called "encode" on some object and not finding it.
I suspect that you are not passing the right duck-typed object to cqlengine for UserImage.image. Does it know how to speak PIL Image objects? I doubt it. I bet cqlengine is looking for the typical python string method .encode (http://docs.python.org/2/library/stdtypes.html#str.encode). Instead of passing the PIL image, try passing just the raw bytes you receive from the form posting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With