我正在尝试将文件(小于100kb)上传到django appengine中的blob字段。但是,我一直收到以下错误:
BadKeyError:无效的字符串键ahVkZXZ-dG9reW8tYW1waG9yYS0zNzlyIgsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxiAgICAgICKCQw /.
以下是我的代码的一部分: models.py
...
name = models.CharField(max_length=200)
cv = blobstore.BlobReferenceProperty()
...
views.py
...
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
@csrf_exempt
def apply(request, posting_id):
title = "Application"
form = None
if request.method == "POST":
cv_blobs = get_uploads(request, field_name="cv", populate_post=True)
form = ApplicationForm(request.POST)
if form.is_valid() and len(blob_info) == 1:
post = Posting.objects.get(id=posting_id)
saves = Candidate(post=post, name=form.cleaned_data['name'], email=form.cleaned_data['email'], years_of_experience=form.cleaned_data['years_of_experience']\
, skillset=form.cleaned_data['skillset'], academics=form.cleaned_data['academics'], number=form.cleaned_data['number']\
, cover_letter=form.cleaned_data['cover_letter'], cv=cv_blobs[0])
saves.put()
return HttpResponseRedirect("/")
if len(cv_blobs) == 0:
request.session['upload_error'] = "cv is required"
form = ApplicationForm(request.POST)
if form is None: form = ApplicationForm()
upload_url = blobstore.create_upload_url('/apply/%s' %posting_id)
params = {
'upload_url': blobstore.create_upload_url('/apply/%s/' %posting_id),
'form': form,
'upload_error': request.session.pop('upload_error', None)
}
return render_to_response("application.html", locals(), context_instance=RequestContext(request))
我做错了什么?
这是FileField方法:
models.py ...
cv = models.FileField(upload_to='uploads/')
...
views.py ...
view_url = '/apply/%s' %post_id
if request.method == "POST":
form = ApplicationForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect("/")
if form is None:
upload_url, upload_data = prepare_upload(request, view_url)
kwargs = {}
kwargs['posting'] = post
form = ApplicationForm(**kwargs)
...
但我仍然得到同样的错误。
答案 0 :(得分:0)
使用blobstore有点牵扯。正式的方法是使用FileField。
请在此处查看django-nonrel文档: http://www.allbuttonspressed.com/projects/django-filetransfers
它应该比你正在做的更干净。