以下是我的观点:
def GeneralUserPictureChange(request, pk, username):
thumb = GeneralUser.objects.get(pk=pk)
if thumb.username == request.user:
if request.method == 'POST':
form = GeneralUserPictureChangeForm(request.POST, request.FILES)
if form.is_valid():
thumb.thumbnail = form.cleaned_data['thumbnail']
thumb.save()
return redirect("user_profile", pk, username)
else:
return render_to_response("gnu_picture_change.html", {"form":form, "basic_info":thumb}, context_instance=RequestContext(request))
else:
form = GeneralUserPictureChangeForm()
return render_to_response("gnu_picture_change.html", {"form":form, "basic_info":thumb}, context_instance=RequestContext(request))
当我这样做时,错误没有返回HttpResponse对象。但是当我删除该行
if thumb.username == request.user:
继续正确的缩进,它不会给出错误..
需要帮助......
答案 0 :(得分:4)
您在这里比较username
字段和user
对象:
if thumb.username == request.user:
thumb.username
可能会返回字符串,例如'JohnDoe'
request.user
返回类似:<User: johndoe>
所以if
似乎是假的 ALLWAYS
要解决此问题,您应该:
if thumb.username == request.user.username:
if thumb.username...
你得到的是None而不是HttpResponse,因为如果第一个if
为假,你就没有回复
def GeneralUserPictureChange(request, pk, username):
thumb = GeneralUser.objects.get(pk=pk)
if thumb.username == request.user:
if request.method == 'POST':
form = GeneralUserPictureChangeForm(request.POST, request.FILES)
if form.is_valid():
thumb.thumbnail = form.cleaned_data['thumbnail']
thumb.save()
return redirect("user_profile", pk, username)
else:
return render_to_response("gnu_picture_change.html", {"form":form, "basic_info":thumb}, context_instance=RequestContext(request))
else:
form = GeneralUserPictureChangeForm()
return render_to_response("gnu_picture_change.html", {"form":form, "basic_info":thumb}, context_instance=RequestContext(request))
# You need a return like this one, in case the first if is false
return render_to_response("gnu_picture_change.html", {"Error":'Not valid username'}, context_instance=RequestContext(request))