2

Here is my view :

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))

When I do this gives the error didnt return HttpResponse object. But when I remove the line if thumb.username == request.user: and continue with proper indentation it dont gives the error..

Need help ...

2
  • 1
    What happens when if thumb.username == request.user: is true and the code goes in that direction? Commented Nov 14, 2014 at 9:32
  • The question is what happens when the if is false, there is no return Commented Nov 14, 2014 at 9:47

1 Answer 1

4

You're comparing an username field and an user object here:

if thumb.username == request.user:
  • thumb.username probably return a string like 'JohnDoe'
  • request.user return a object like: <User: johndoe>

So the if seems to be false ALLWAYS

To fix this, you should do:

if thumb.username == request.user.username:
  • What happen if the first condition is false ? You need an alternative return, same indent as the if thumb.username...

You're getting the None instead of a HttpResponse, because you don't have a return in case the first if is false

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))
Sign up to request clarification or add additional context in comments.

1 Comment

In the last return you should only return an error variable or intialize an empty form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.