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...