CustomUser对象没有属性“方法” Django 3.0

时间:2019-12-08 20:03:18

标签: django python-3.x django-templates django-views

我正在使用login()和此导入from django.contrib.auth import authenticate, login进行登录视图。该文档说,登录有两个参数,但是当我在执行login(request, user)之后使用user = authenticate(username=username, password=raw_password)时,却告诉我login()接受一个位置参数却得到2。当我使用login(user)时它告诉我CustomUser object has no attribute 'method'

使用login(request, user)

时出错
TypeError at /user_accounts/login

login() takes 1 positional argument but 2 were given

视图

def login(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('home')
        else:
            messages.error(request,'username or password not correct')
            return redirect('login')
    else:
        return render(request, 'user_accounts/login.html')

模板

<!DOCTYPE html>
<html>

<head>
    <title>Login</title>
</head>

<body>
    <form method="POST">
        {% csrf_token %}
        <p>
            <label>Username</label>
            <input type="text" name="username">
        </p>
        <p>
            <label>Password</label>
            <input type="password" name="password">
        </p>
        <button type="submit">Login</button>
    </form>
</body>

</html>

url

url(r'^user_accounts/login', account_views.login, name="login"),

0 个答案:

没有答案