Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-crispy-forms AttributeError when using built in AuthenticationForm

I'm trying to use django-crispy-forms to display the built-in AuthenticationForm with the login view in django. I'm having issues subclassing AuthenticationForm - I'm getting an AttributeError. The error says 'WSGIrequest' object has no attribute 'get'. Here is my form:

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="username"),
            Field('password', placeholder="password"),)
        super(AuthenticationForm, self).__init__(*args, **kwargs)

I think this error has to do with the login view being called via get from a redirect (I'm using the @login_required decorator). Does anyone have any ideas on how to subclass built in forms with django-crispy-forms and avoid this error?

like image 371
bgmaster Avatar asked Jan 17 '26 18:01

bgmaster


1 Answers

It looks like you've got an error in your form:

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('username', placeholder="username"),
            Field('password', placeholder="password"),
        )

You are calling super, passing parent class AuthenticationForm not LoginForm.

like image 94
maraujop Avatar answered Jan 20 '26 10:01

maraujop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!