0

This question might appear to be a duplicate and/or too boring. I have already read this, this, this, this and this questions & answers. But couldn't find solution that fits to my problem.

I'm new to Django framework. To learn it I want to create simple blog. When user clicks Register button (after filling required fields), following error thrown:

ValueError at /user/register/ The view user.views.register didn't return an HttpResponse object. It returned None instead.

views.py

def register(request):
    """
    Registers new user. 
    """
    if request.POST:
        if request.method == 'POST':
            personal_info = UserFormModel(request.POST)
            if personal_info.is_valid():
                email = personal_info.cleaned_data['email']
                username = personal_info.cleaned_data['username']
                if User.objects.filter(email=email).exists():
                    return HttpResponse('email error')
                elif User.objects.filter(username=username).exists():
                    return HttpResponse('username error')
                else:
                    return HttpResponse('saved')
    else:
        personal_info = UserFormModel()
        return render_to_response('user/registration.html',
                            {
                                'title': 'Registration',
                                'username_error': 'Sorry, someone already has that username.',
                                'personal_info': personal_info,
                            }, 
                            context_instance=RequestContext(request))

If necessary I can share any files content.

Any helpful comment or answer would be appreciated.

Community
  • 1
  • 1
Mirjalal
  • 1,292
  • 1
  • 17
  • 40

1 Answers1

0

In line if personal_info.is_valid():

if personal info is not valid, it will return None.(return nothing)

add an else condition, because you are not handling a case.

if personal_info.is_valid():
    # code here
else:
    return HttpResponse('personal info provided not valid')

One of the better ways to handle situations like this from not occurring is to keep a dictionary for status message and result initialised at the start of the function, and return only at a single place instead of returning at multiple places, and storing result in result_dict.

result_dict = {'status': False}

and always return at the end of the function

return HttpResponse(json.dumps(result_dict))

This way you will never miss a case in returning values.

So Final code should look like

def register(request):
    """
    Registers new user. 
    """
    result_dict = {'status': False}
    if request.POST:
        if request.method == 'POST':
            personal_info = UserFormModel(request.POST)
            if personal_info.is_valid():
                email = personal_info.cleaned_data['email']
                username = personal_info.cleaned_data['username']
                if User.objects.filter(email=email).exists():
                    result_dict['message'] = 'email error'
                elif User.objects.filter(username=username).exists():
                    result_dict['message'] = 'username error'
                else:
                    result_dict['message'] = 'saved'
                    result_dict['status'] = True
        else:
            result_dict['message'] = 'personal info provided not valid'
    else:
        personal_info = UserFormModel()
        return render_to_response('user/registration.html',
                            {
                                'title': 'Registration',
                                'username_error': 'Sorry, someone already has that username.',
                                'personal_info': personal_info,
                            }, 
                            context_instance=RequestContext(request))

    return HttpResponse(json.dumps(result_dict))
Namit Singal
  • 1,506
  • 12
  • 26