The view didn't return an HttpResponse object. It returned None instead. Request Method: POST
Question: The problem appears when I submit the form. A few days ago it worked, I
changed nothing in this code, but now it fails. Is it possible that different
module makes that part of code crashed? Traceback:

Environment:

    
    
    Request Method: POST
    Request URL: http://127.0.0.1:8000/users/new/
    
    Django Version: 1.7.3
    Python Version: 3.4.0
    Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'mojblog',
     'users')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')
    
    
    Traceback:
    File "/home/marta/django/djangovenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
      130.                                  % (callback.__module__, view_name))
    
    Exception Type: ValueError at /users/new/
    Exception Value: The view users.views.create didn't return an HttpResponse object. It returned None instead.
    

Code:

    
    
    from django.shortcuts import render, redirect
    from django.http import HttpResponse
    from users.forms import UserCreationForm
    from django.contrib.auth.models import User
    from users.models import Profile
    
    
    def test(request):
        return HttpResponse('users!')
    
    
    def create(request):
        if request.method =="POST":
            new_user=User()
            form = UserCreationForm(request.POST, instance=new_user)
            if form.is_valid():
                new_user=form.save(commit=False)
                new_user.is_active=False
                new_user.save()
                return redirect("users:new")
    
        else:
            form = UserCreationForm()
            return render(request, 'users/create.html', {'form1': form})
    
    
        # else:
        #   return HttpResponse("something")        
    
    def activate(request, key):
    
        profile=Profile.objects.get(secret=key)
        user=profile.user
        user.is_active=True
        user.save()
    
    
    
        return render(request, 'users/activate.html', {})
    


Answer: In the `create` view, if the request is a POST but the form is not valid, the
code does not return anything.

Rather than have a separate GET block, you should use the standard pattern:

    
    
    if request.method =="POST":
        form = ...
        if form.is_valid():
            ...
            return redirect(...)
    else:
        form = ...
    return render(request, 'users/create.html', {'form1': form})
    

In this case the final return catches that case, and re-renders the form so
that it displays the errors.

