请帮助解决问题。
我使用上面的表单加载文件。继承人view.py:
def change_avatar(request):
entry_user_profile = UserProfile.objects.get(user_ptr_id=request.user.id)
avatar = entry_user_profile.avatar
form = ChangeAvatarForm(instance=entry_user_profile)
if request.method == 'POST' and request.is_ajax():
form = ChangeAvatarForm(request.POST, request.FILES, instance=entry_user_profile)
if form.is_valid():
form.save()
for filename, blob in request.FILES.iteritems():
name = request.FILES[filename].name
data = {'sourceImg': name[0]}
print(name[0])
return HttpResponse(json.dumps(data), content_type='application/json')
t = loader.get_template('page_change_avatar.html')
c = RequestContext(request, {
'form': form,
'avatar': avatar,
}, [custom_proc])
return HttpResponse(t.render(c))
问题是没有字典"名称"文件名。日志中的错误消息如下:
AttributeError:' MultiValueDict'对象没有属性' iteritems'
P3.4 / D1.6
答案 0 :(得分:0)
仅使用iterateitems()
替换items()
。
def change_avatar(request):
entry_user_profile = UserProfile.objects.get(user_ptr_id=request.user.id)
avatar = entry_user_profile.avatar
form = ChangeAvatarForm(instance=entry_user_profile)
if request.method == 'POST' and request.is_ajax():
form = ChangeAvatarForm(request.POST, request.FILES, instance=entry_user_profile)
if form.is_valid():
form.save()
#changed here
for filename, blob in request.FILES,items():
name = request.FILES[filename].name
data = {'sourceImg': name[0]}
print(name[0])
return HttpResponse(json.dumps(data), content_type='application/json')
t = loader.get_template('page_change_avatar.html')
c = RequestContext(request, {
'form': form,
'avatar': avatar,
}, [custom_proc])
return HttpResponse(t.render(c))