0
votes

I am getting this error and I have not been able to fix this. Here are the files:views.py

from django.shortcuts import render,HttpResponse
from django.contrib.auth.forms import UserCreationForm
# Create your views here.
def home(request):
    numbers=[1,2,3,4,5]
    name='satya'
    args={'MYname':name,'numbers':numbers}
    return render(request,'accounts/home.html',args)

def register(request):
    if request.method=='POST':
        form=UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts')

    else:
        form=UserCreationForm()
        args={'form':form}
        return render(request,'accounts/reg_form.html',args)    

Here is reg_form.html

{% extends 'base.html' %}

{% block body %}
<div class="container">
    <h1> Register </h1>
    <form method="post">
    {% csrf_token %}
    {{form.as_p}}
    <<button type=" submit " class="submit" >Submit</button>


</form>
</div>
{% endblock %}

I have imported the things correctly but not getting as what is wrong.

1

1 Answers

2
votes

The problem is that you are not returning a response for POST requests when the form is invalid. You can fix this by unindenting the last two lines so that they run for GET and POST requests.

def register(request):
    if request.method=='POST':
        form=UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts')

    else:
        form=UserCreationForm()
    args={'form':form}
    return render(request,'accounts/reg_form.html',args)