Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django login() takes 1 positional argument but 2 were given

Tags:

python

django

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

        })
like image 369
thomaSmith Avatar asked Oct 21 '17 12:10

thomaSmith


People also ask

How do you fix takes 1 positional argument but 2 were given?

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.

How does Django authentication work?

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.


1 Answers

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)
like image 74
Brown Bear Avatar answered Sep 19 '22 03:09

Brown Bear