I created a form for login, just like this:
class LoginForm(AuthenticationForm):
username = forms.CharField (label=_("Usuario"), max_length=30,
widget=forms.widgets.
TextInput(attrs={'id':'username','maxlength':'25'}))
password = forms.CharField (label=_("Password"), widget=forms.widgets.
PasswordInput(attrs={'id':'password','maxlength':'10'}))
I use it in this view:
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
...
After debugging I realize that the form.is_valid() method returns false cause the is_bound attr is false. Do I have to redefine something in my form or to modify my view???
Edit 1
I have followed this SO question about is_valid() method returning False:
form.is_valid() always returning false
but the problem is still there.
The issue is actually similar to the one in the question you link to. The form you’re inheriting from,
django.contrib.auth.forms.AuthenticationForm, takesrequestas its first parameter, before the usualdataparam. This is why your form is reporting that it is not bound – as far as it’s concerned, you’re not passing in any data, so it has nothing to bind to.So, in your view, you’ll need to instantiate it like this: