1

我正在制作一个网站来存储书籍详细信息。我有这个带有照片字段的模型书。我正在使用谷歌应用引擎来部署这个网站。要存储我正在使用 blobstore 的图像。我得到的“照片”是一个无效的关键字参数对于此功能错误.....无法弄清楚出了什么问题。

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'
4

1 回答 1

0

尝试先保存d.save()不带照片字段的模型 ( ),然后仅在保存后更新照片字段。这应该类似于:

d = book(selleruserid=request...) # No photo field here
d.save()
d.photo = photo_blobs[0]
d.save()
于 2013-07-18T05:54:15.247 回答