I am using the latest version of django and python 3, When I log in I get the below error message.
django login() takes 1 positional argument but 2 were given
Please find the code for my login view below.
from django.shortcuts import render, get_object_or_404,redirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from authentication.forms import LoginForm, ForgottenPasswordForm, ResetPasswordForm
from authentication.functions import send_user_reset_password_link, resend_password_reset_link
from authentication.models import ResetPassword
# Create your views here.
def login(request):
error_message = None
heading = 'Login Form'
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
remember_me = form.cleaned_data['remember_me']
user = authenticate(request,username=username, password=password)
if not request.POST.get('remember_me', None):
#request.session.set_expiry(0)
if user is not None:
login(request, user)
return redirect('property_index',user.id)
# A backend authenticated the credentials
else:
error_message = 'No login credentials found'
# No backend authenticated the credentials
form = LoginForm()
return render(request,'authentication/forms/login.html',{
'form':form,
'error_message':error_message,
'heading':heading
})
To solve this ” Typeerror: takes 1 positional argument but 2 were given ” is by adding self argument for each method inside the class. It will remove the error.
The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.
The trouble is: you override the original django login function. So you should change import.
from django.contrib.auth import authenticate, login as dj_login
# ^^^^^^^^
and use
dj_login(request, user)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With