7

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 ')]
Zoe
  • 27,060
  • 21
  • 118
  • 148
AJAY RAWAT
  • 81
  • 1
  • 1
  • 3

5 Answers5

12

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

path('login',views.login_view, name = 'login ')
scharette
  • 9,437
  • 8
  • 33
  • 67
0
from django.urls import path, include
from django.contrib.auth import views

urlpatterns = [
path('',views.register, name='register' ),
path('form',views.form, name = 'form'),
path('login/', views.login, {'template_name': 'login.html'}, name='login'),
]

or

from django.urls import path, include
from django.contrib.auth import views as auth_views

urlpatterns = [
path('',views.register, name='register' ),
path('form',views.form, name = 'form'),
path('login/', auth_views.login, {'template_name': 'login.html'}, name='login'),
]

If you are using django's own authentication scheme, the second example will be more accurate.

I actually changed the login path in the url file. I think the error is due to the path connection. that's path =>

path('login',views.login, name = 'login ')]
Ömer
  • 121
  • 1
  • 4
  • 19
0

Django built in user authentication only accepts username and password to login. You have to change it if you want to log in your user by email and password.

Sam
  • 815
  • 10
  • 23
0

You declared your function as login_view in views.py but you are calling it as login on urls.py. Try view.login_view.

Gesy Darati
  • 113
  • 1
  • 6
0

In your code, it seems that you have a function named login_view defined, but in your URL configuration, you are referring to a function named login. This mismatch in function names is likely causing the error.

Change the function name in your URL configuration to match the view function name,

urlpatterns = [
    # other URL patterns
    path('login', views.login_view, name='login'),
]