我正在建立一个网站来存储书籍详细信息。我有这个模型书与照片字段。我正在使用谷歌应用程序引擎部署这个网站。存储图像我正在使用blobstore.I我得到'照片'是一个这个函数的错误关键字参数错误.....无法弄清楚出了什么问题。
def addbook(request):
if request.user.is_authenticated():
results=book.objects.filter(selleruserid=request.session["user_id"]).order_by('-id');
if request.method == 'POST':
photo_blobs =get_uploads(request, field_name='image',populate_post=True)
form = addbookform(request.POST,request.FILES)
if form.is_valid() and len(photo_blobs) == 1:
cd=form.cleaned_data
d = book(photo=photo_blobs[0],selleruserid=request.session["user_id"],mrp=cd['mrp'],categories=cd['categories'],branch=cd['branch'],flag="Available",bookname=cd['bookname'],author=cd['author'],publisher=cd['publisher'],rentprice=cd['rentprice'],price=cd['price'],description=cd['description'],genre=cd['genre'])
d.save()
return render_to_response('sellbook.html',{'notif':request.session["notif"],'count':request.session["count"],'user':request.user,'form':form,'notif1':request.session["notif1"],'results':results},context_instance=RequestContext(request))
else:
form = addbookform()
results = book.objects.filter(selleruserid=request.session["user_id"])
return render_to_response('sellbook.html',{'upload_url':blobstore.create_upload_url('/sellbook'),'user':request.user,'notif':request.session["notif"],'count':request.session["count"],'form':form,'notif1':request.session["notif1"],'results':results}, context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/redirecting')
def get_uploads(request, field_name=None, populate_post=False):
"""Get uploads sent to this handler.
Args:
field_name: Only select uploads that were sent as a specific field.
populate_post: Add the non blob fields to request.POST
Returns:
A list of BlobInfo records corresponding to each upload.
Empty list if there are no blob-info records for field_name.
"""
if hasattr(request,'__uploads') == False:
request.META['wsgi.input'].seek(0)
fields = cgi.FieldStorage(request.META['wsgi.input'], environ=request.META)
request.__uploads = {}
if populate_post:
request.POST = {}
for key in fields.keys():
field = fields[key]
if isinstance(field, cgi.FieldStorage) and 'blob-key' in field.type_options:
request.__uploads.setdefault(key, []).append(blobstore.parse_blob_info(field))
elif populate_post:
request.POST[key] = field.value
if field_name:
try:
return list(request.__uploads[field_name])
except KeyError:
return []
else:
results = []
for uploads in request.__uploads.itervalues():
results += uploads
return results
书籍型号:
class book(models.Model):
selleruserid=models.CharField(max_length=30,editable=False,blank=False)
bookname=models.CharField(max_length=100,blank=False)
author=models.CharField(max_length=20,blank=False)
publisher=models.CharField(max_length=10,blank=True)
price=models.IntegerField(max_length=25,blank=True,null=True)
rentprice=models.IntegerField(max_length=25,blank=True,null=True)
photo= blobstore.BlobReferenceProperty()
description=models.CharField(max_length=200,blank=True,null=True)
mrp=models.IntegerField(max_length=10,blank=True,null=True)
categories=models.CharField(max_length=20,choices=BOOK_CHOICES)
genre=models.CharField(max_length=20,choices=GENRE_CHOICES,blank=True)
branch=models.CharField(max_length=20,choices=BRANCH_CHOICES,blank=True)
flag=models.CharField(max_length=10,editable=False,choices=FLAG_CHOICES)
class Meta:
db_table = 'book'
答案 0 :(得分:0)
首先尝试保存不带照片字段的模型(d.save()
),并且只有在保存后才更新照片字段。这应该类似于:
d = book(selleruserid=request...) # No photo field here
d.save()
d.photo = photo_blobs[0]
d.save()