Tuesday, November 28, 2017

Why does Django say my login form is invalid? How can I find out why Django thinks it is?

My `forms.py` looks like this. (I want user's email to be their login username. 
from django.utils.translation import ugettext_lazy as _

from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User

class LoginForm(AuthenticationForm):
 username
= forms.EmailField(label=_("Email"), max_length=254)


 
class Meta:
 model
= User
 fields
= ("username",)

`views.py`:
def login_register(request, template="pages/login_register.html"):
 
if request.method=="POST":
  login_form
= LoginForm(request.POST)
  
if login_form.is_valid():
   
print "Login form valid"
   
return redirect(home_slug())
  # else:
  #  print "Login invalid"
 
else:
  login_form
= LoginForm()
 
return render(request, template, {"login_form": login_form})


`login_register.html`:
 <form method="post">
  {% csrf_token %}
  {{ login_form.as_p }}
  
<button type="submit">Log in</button>
 
</form>

The login form accepts two fields labeled "Email" and "Password." But when I hit "Log in" button, the page just seems to refresh. If I uncomment the `print` statement, it prints, indicating that `login_form.is_valid()!=True`. Why does Django do this?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e861f42-2115-4377-848c-67c59d122610%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment