我想将我的网站图像存储在 cassandra 数据库中!我必须读取和存储图像字节。你有一个不错的代码给我吗?我正在使用python2.7、django框架和cql-engine!
这是我的代码:
1-我的模型:
from cqlengine import columns
from cqlengine.models import Model
class UserImage(Model):
Email = columns.Text(primary_key=True)
image=columns.Bytes(required=False)
2- 我的表格:
class UserImage(forms.Form):
image=forms.ImageField()
3- 我的观点:
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='ebrahimi.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))
我的模板:
{% block content %}
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
{% endblock %}
我在渲染模板中的错误:
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
你有什么想法?请指导我...