[Answer]-Django UserCreationForm not rendering errors in template

1👍

You are explicitly recreating the form if it has errors, replacing it in the context with one that is unbound and therefore doesn’t have errors. Don’t do that. Your view should be simply:

def home(request):
    if request.method =='POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Thank you for joining!')
            return HttpResponseRedirect('thank-you')

    else:
        form = MyRegistrationForm()

    return render(request, 'signup.html', {'form': form})

Note the other changes: always redirect after a successful post; render rather than render_to_response, since it creates a RequestContext for you; and no need to add the user or csrf values yourself, since they are added by the context processors as long as you do use a RequestContext.

Leave a comment