Django“ login () รับข้อผิดพลาด 1 อาร์กิวเมนต์ (ให้ 2 ตัว)”


86

ฉันพยายามจัดเก็บ ID ของผู้ใช้ในเซสชันโดยใช้ django.contrib.auth.login แต่มันไม่ทำงานไม่เป็นไปตามที่คาดไว้

ฉันได้รับข้อผิดพลาด เข้าสู่ระบบ () รับ 1 อาร์กิวเมนต์ (ให้ 2)

ด้วยการเข้าสู่ระบบ (ผู้ใช้) ฉันได้รับ AttributeError ที่ / login / User 'object ไม่มีแอตทริบิวต์' method '

ฉันใช้แบบฟอร์มตัวอย่างที่แก้ไขเล็กน้อยhttp://docs.djangoproject.com/en/dev/topics/auth/ :

from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login

def login(request):
    msg = []
    if request.method == 'POST':
        username = request.POST['u']
        password = request.POST['p']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                msg.append("login successful")
            else:
                msg.append("disabled account")
        else:
            msg.append("invalid login")
    return render_to_response('login.html', {'errors': msg})

ไม่มีอะไรพิเศษเกี่ยวกับ login.html:

<html>
<head>
    <title></title>
</head>
<body>
    <form action="/login/" method="post">
        Login:&nbsp; <input type="text" name="u">
    <br/>
        Password:&nbsp; <input type="password" name="p">
        <input type="submit" value="Login">
    </form>
    {% if errors %}
        <ul>
            {% for error in errors %}
            <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}

</body>
</html>

ใครมีความคิดที่จะทำให้การเข้าสู่ระบบ () ทำงาน


4
จะใช้งานได้หากคุณเปลี่ยนชื่อมุมมองของคุณ
Evgeny

เพียงแค่คำถามและคำตอบที่ฉันกำลังมองหา หนึ่งในข้อผิดพลาดหลายประการที่ฉันทำขณะพยายามเข้าสู่ระบบและทำงานใน django พร้อมกับการใช้ Contexts vs RequestContexts และออกจาก csrf_tokens
chucksmash

ที่นี่คุณสามารถดูบทช่วยสอนเกี่ยวกับผู้ใช้ Django lowcoupling.com/post/71289666862/django-the-user-tutorialนอกจากนี้ยังมีโครงการ GitHub ที่คุณสามารถโคลนและตรวจสอบได้
lowcoupling

คำตอบ:


235

ฟังก์ชั่นมุมมองของคุณถูกเรียกloginด้วยและการเรียกเพื่อlogin(request, user)ลงท้ายจะถูกตีความว่าเป็นการพยายามเรียกใช้ฟังก์ชันนี้แบบวนซ้ำ:

def login(request):
    ...
    login(request, user)

เพื่อหลีกเลี่ยงไม่ให้มันเปลี่ยนชื่อฟังก์ชั่นมุมมองของคุณหรืออ้างถึงloginจากdjango.contrib.authด้วยวิธีอื่น ตัวอย่างเช่นคุณสามารถเปลี่ยนการนำเข้าเพื่อเปลี่ยนชื่อฟังก์ชันการเข้าสู่ระบบ:

from django.contrib.auth import login as auth_login

...
auth_login(request, user)

ว้าวฉันกำลังเรียกดูซอร์สโค้ดของ Django และลองใช้เวอร์ชันเก่าทุกเวอร์ชันคิดว่าฉันจะบ้าไปแล้ว
Mojimi

16

การแก้ไขที่เป็นไปได้อย่างหนึ่ง:

from django.contrib import auth

def login(request):
    # ....
    auth.login(request, user)
    # ...

ตอนนี้ชื่อมุมมองของคุณไม่ได้เขียนทับชื่อมุมมองของ django


9

อีกวิธีหนึ่ง:

from django.contrib.auth import login as auth_login

แล้วโทรauth_login(request, user)แทนlogin(request, user).

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.