Errors in form with ModelForm

I'm not good in English

I want to have a login page, I use ModelForm and LoginView.

when I want to see the page I got this error:

BaseModelForm.__init__() got an unexpected keyword argument 'request'

and when I don't use the Form, always return to the same page. I checked the request.method, it's GET!

here is my form:

class LoginForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)

    class Meta:
        model = User
        fields = ['username', 'password']

view:

class Login(LoginView):
    form_class = LoginForm
    template_name = 'account/login.html'

    def get_success_url(self):
        return reverse_lazy('account:home')

and login.html

            <form action="{% url 'account:home' %}" method="post">
                {% csrf_token %}
                <div class="input-group mb-3">
                    <input id="username" type="text" class="form-control" placeholder="Username">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-user"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input id="password" type="password" class="form-control" placeholder="Password">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-lock"></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <!-- /.col -->
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
                    </div>
                    <!-- /.col -->
                </div>
            </form>

when I use my form(with function view), my browser don't suggest my saved logins.

The LoginView is a bit tricky: it passes the request to the form to let the ModelForm log in the user, that needs to be removed from the kwargs in the super call, like:

class LoginForm(ModelForm):
    def __init__(self, *args, request=None, **kwargs):
        super().__init__(*args, **kwargs)

    class Meta:
        model = User
        fields = ['username', 'password']

currently, your LoginForm will of course not log in the user, and therefore not do much.


Note: Since PEP-3135 [pep], you don't need to call super(…) with parameters if the first parameter is the class in which you define the method, and the second is the first parameter (usually self) of the function.

Back to Top