Question

clean is not showing errors on my login form regardless of what my input is and regardless of what method of displaying errors I try in clean.

In my CustomUserCreationForm error displaying works perfectly. The only difference between the two is login extends forms.Form while Custom extends UserCreationForm

Also I'm using django-crispy-forms to render my forms

class LoginForm(forms.Form):
    username = forms.CharField(label=('UserName'),
            widget = forms.TextInput(attrs={'placeholder': _('Username')})
    )
    password = forms.CharField(label=('Password'),
            widget=forms.PasswordInput(attrs={'placeholder' : _('Password') }),
    )

    def helper(self):
            helper = FormHelper()

            helper.form_id = "Login"
            helper.form_method = "POST"
            helper.layout = Layout(Div(
                    Field('username', css_class='input-box-rounded'),
                    Field('password', css_class='input-box-rounded'),
                    Submit('Login', 'Login',  css_class='col-md-6 col-md-offset-3 rounded'),
                     css_class='col-md-4 col-md-offset-4 centered-div'))
            return helper

    def clean(self):

            cleaned_data = super(LoginForm, self).clean()

            if 'username' not in cleaned_data:
                    msg = _("Please enter a username")
                    self._errors['username'] = self.error_class([msg])
            if 'password' not in cleaned_data:
                    msg = _("Please enter a password")
                    raise forms.ValidationError(msg)
            u =authenticate(username = cleaned_data['username'], password = cleaned_data['password'])
            if u == None:
                    msg = _("Username or Password is incorrect")
                    self.add_error('username', msg)

            return cleaned_data
Was it helpful?

Solution

Can you post your view and template code? Without seeing either of those, I assume either your template needs to display the errors or your view is not handling the form, though I haven't used Django Crispy Forms.

{{ form.non_field_errors }}
{{ form.username.errors }}

FYI, the preferred way to handle error checking is to create a clean function for each field and have it raise a ValidationError when there is a problem. This will then be a field error (second line above).

def clean_password(self):
    data = self.cleaned_data.get('password')
    if not data:
        raise ValidationError(_("Please enter a password"))

Also, since you're just checking that a field is there, so you could set required=True for each required field and skip the manual validation.

class LoginForm(forms.Form):
    username = forms.CharField(label=('UserName'), required=True,
        widget = forms.TextInput(attrs={'placeholder': _('Username')})
    )
    password = forms.CharField(label=('Password'), required=True,
        widget=forms.PasswordInput(attrs={'placeholder' : _('Password') }),
    )

See the documentation for more info: https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top