django login

django login,第1张

目录
  • 问题:
  • Answer 1
  • 总结:

问题:

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

        })
Answer 1

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)
总结:

会话保存的login(request, user, backend=None)和文件内登录函数login(request)重名了。

参考链接:

Django自定义的模型如何使用内置的login方法? ٩( ‘ω‘ )و Django问题

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/915551.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存