Reputation: 300
I have a very simple sign up template in django. This form is just not working, wether it's valid or not.
the html code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="POST" action="">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{{ form.errors }}
</body>
</html>
the view:
def test(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
redirect('index')
form = UserCreationForm()
return render(request, 'test.html', {'form': form})
I expect to get redirected back to the index page if the form is valid, and get the form errors if it's not valid. I end up not getting anything in return - only the chrome prompt that suggests saving my details.
Upvotes: 0
Views: 261
Reputation: 11
Because you didn't loop the errors properly.
{% if form.errors %} {% for field in form %} {% for error in field.errors %}<p> {{ error }}</p> {% endfor %}{% endfor %}{% endif %}
Upvotes: 1