login() missing 1 required positional argument: 'user'

AJAY RAWAT picture AJAY RAWAT · Jul 26, 2018 · Viewed 8.7k times · Source

Here is my view,

def login_view(request) :
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
          email = form.cleaned_data['email']
          password = form.cleaned_data['password']
          user = authenticate(email=email, password=password)

          if user is not None:
              form = LoginForm()
              login(request, user)

I get an error:

login() missing 1 required positional argument: 'user'

Also, I am using a custom user model

My urls.py:

urlpatterns = [
path('',views.register, name='register' ),
path('form',views.form, name = 'form'),
path('login',views.login, name = 'login ')]

Answer

scharette picture scharette · Jul 26, 2018

You're not using the right view, try this instead

path('login',views.login_view, name = 'login ')