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

72 views
Skip to first unread message

Tom Tanner

unread,
Nov 28, 2017, 9:54:26 PM11/28/17
to Django users
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?

Matemática A3K

unread,
Nov 29, 2017, 12:21:04 AM11/29/17
to django...@googlegroups.com
class LoginForm(AuthenticationForm):
 username
= forms.EmailField(label=_("Email"), max_length=254)


 
class Meta:
 model
= User
 

 fields
= ("username",)


If you use "fields = ("username")" you are restricting the fields to just that field, and the authentication form needs also password to be valid
 

--
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...@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.

Tom Tanner

unread,
Nov 29, 2017, 10:35:53 PM11/29/17
to Django users
What would I need to change? 

I tried changing the ...
fields= ("username",)

... to ...
fields= ("username","email",)
or...
fields= ("email",)

Nothing seemed to change. the `login_form.is_valid()` still is `False`.

Sorry if the question is dumb, I'm still learning Django thru working with it.



On Wednesday, November 29, 2017 at 12:21:04 AM UTC-5, Matemática A3K wrote:
class LoginForm(AuthenticationForm):
 username
= forms.EmailField(label=_("Email"), max_length=254)


 
class Meta:
 model
= User
 

 fields
= ("username",)


If you use "fields = ("username")" you are restricting the fields to just that field, and the authentication form needs also password to be valid
 

--
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...@googlegroups.com.

Matemática A3K

unread,
Nov 29, 2017, 11:58:28 PM11/29/17
to django...@googlegroups.com
On Thu, Nov 30, 2017 at 12:35 AM, Tom Tanner <dontsende...@gmail.com> wrote:
What would I need to change? 

I tried changing the ...
fields= ("username",)

... to ...
fields= ("username","email",)
or...
fields= ("email",)

Nothing seemed to change. the `login_form.is_valid()` still is `False`.

Sorry if the question is dumb, I'm still learning Django thru working with it.


Try not using fields at all, that will use the fields in AuthenticationForm
 


On Wednesday, November 29, 2017 at 12:21:04 AM UTC-5, Matemática A3K wrote:

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


 
class Meta:
 model
= User
 

 fields
= ("username",)


If you use "fields = ("username")" you are restricting the fields to just that field, and the authentication form needs also password to be valid
 

--
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...@googlegroups.com.
To post to this group, send email to django...@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.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Tom Tanner

unread,
Nov 30, 2017, 12:48:50 AM11/30/17
to Django users
I removed that line, but nothing changed.

Matemática A3K

unread,
Nov 30, 2017, 3:57:06 AM11/30/17
to django...@googlegroups.com
Tom,

- You shouldn't use Model=User (or the Meta) as it was a ModelForm, it is just a regular Form
- AuthenticationForm takes request as its first parameter:

def login_register(request, template="templates/login_register.html"):
    if request.method == "POST":
        login_form = AuthenticationForm(request, data=request.POST)
        if login_form.is_valid():
            print("Login form valid")
            # return redirect(home_slug())

        # else:
        #  print "Login invalid"
    else:
        login_form = AuthenticationForm()

ankita gupta

unread,
Nov 30, 2017, 7:51:47 AM11/30/17
to Django users
Reply all
Reply to author
Forward
0 new messages