Reputation: 5
I am trying to use a Django form for a login page, but the form is returning False whenever I call form.is_valid(). I tried to print the errors in the console and in the HTML file but there's nothing displaying. I tried to get data from the form with form["email"].value() and it's returning None, even when I input data into the email field.
Here's views.py:
def postlogin(request):
form = BootstrapAuthenticationForm(request.POST)
# This prints out None into the console
print(form["email"].value())
if form.is_valid():
# This code never runs! Even when there are no errors in the form fields!
return render(request, "app/home.html")
# If form is invalid, return to login page
# This always runs, meaning form.is_valid() keeps being false!
return render(request,"app/login.html", {"form":form})
Here's my forms.py:
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
class BootstrapAuthenticationForm(AuthenticationForm):
"""Authentication form which uses boostrap CSS."""
email = forms.CharField(max_length=254,label=('Email'),
widget=forms.TextInput({
'class': 'form-control',
'placeholder': 'Email'}))
password = forms.CharField(label=("Password"),
widget=forms.PasswordInput({
'class': 'form-control',
'placeholder':'Password'}))
Here's my login.html:
{% extends "app/layout.html" %}
{% block content %}
<h2>{{ title }}</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
<form action="/postlogin/" method="post" class="form-horizontal">
{% csrf_token %}
<h4>Use a local account to log in.</h4>
<hr />
<div class="form-group">
<label for="id_username" class="col-md-2 control-label">{{form.email.label}}</label>
<div class="col-md-10">
{{ form.email }}
</div>
</div>
<div class="form-group">
<label for="id_password" class="col-md-2 control-label">{{form.password.label}}</label>
<div class="col-md-10">
{{ form.password }}
</div>
</div>
<div class="form-group">
<label for="id_password" class="col-md-2 control-label">{{form.test.label}}</label>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="hidden" name="next" value="/" />
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
<!-- Nothing below shows even when form.is_valid() is false -->
{% if form.errors %}
<p class="validation-summary-errors">Please enter a correct user name and password.</p>
{% endif %}
{% if form.non_field_errors %}
<p class="validation-summary-errors">Please enter a correct user name and password.</p>
{% endif %}
</form>
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm"></section>
</div>
</div>
<!-- Again, none of this shows in the HTML file even when form.is_valid() is False -->
{% for error_field, error_message in form.errors.items %}
{{ error_field|striptags }}: {{ error_message|striptags }}
{% endfor %}
{% endblock %}
Upvotes: 0
Views: 94
Reputation: 3650
if form.is_valid():
# after validating form you can print field value using (cleaned_data) method
print(form.cleaned_data['email'])
return render(request, "app/home.html")
Upvotes: 0