'用户'对象没有属性'方法'

问题描述 投票:0回答:1

我正在玩Django登录和注销阶段。我有一个登录页面,如果用户使用'GET'获取它,他会获得表单,如果他提交表单,它将被提交给相同的view.login函数。问题是,当我尝试使用' if request.method=='POST'。我得到一个错误,'User has not attribute method',我不明白,为什么'User'对象传递而不是request对象?这是views.py的代码:

from django.shortcuts import render
from django.http import HttpResponse, Http404, HttpResponseRedirect
from .models import Flight,Passenger
from django.urls import reverse
from django.contrib.auth import authenticate,login, logout
from django.contrib.auth.models import User


def index(request):
    if not request.user.is_authenticated:
        return render(request,"login.html")

    context={
        "flights":Flight.objects.all()
    }
    return render(request,"index.html",context)

def flight(request,flight_id):
    flight= Flight.objects.get(pk=flight_id)
    passengers = flight.passengers.all()
    non_passengers = Passenger.objects.exclude(flight=flight).all()
    context={
    "flight":flight,
    "passengers":passengers,
    "non_passengers":non_passengers
    }
    return render(request,"flight.html",context)

def book(request,flight_id):
    passenger_id = int(request.POST["passenger"])
    flight = Flight.objects.get(pk=flight_id)
    passenger = Passenger.objects.get(pk=passenger_id)
    passenger.flight.add(flight)
    return HttpResponseRedirect(reverse("flight",args=(flight_id,)))

def login(request):
    print("\n In LOGIN \n")
    if request.method=='POST':
        print("\n In POST Request \n")
        username = request.POST["name"]
        password = request.POST["password"]
        user = authenticate(request,username=username,password=password)
        if user is not None:
            login(user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request,"login.html")

这是登录页面:

<html>
<head>
<meta charset="utf-8">

</head>
<body>
    <form action="{% url 'login' %}" method="POST">
        {% csrf_token %}
        Name<input type="text" name="name">
        Password<input type="password" name="password">
        <button type="submit"> Submit </button>
    </form>
</body>


</html

这是错误消息(不是完整消息):

AttributeError at /flights/login

'User' object has no attribute 'method'

Request Method:     POST
Request URL:    http://127.0.0.1:8000/flights/login
Django Version:     2.0
Exception Type:     AttributeError
Exception Value:    

'User' object has no attribute 'method'

Exception Location:     E:\Web\web dev\Practice-code\mysite\flights\views.py in login, line 38
Python Executable:  C:\Users\Mazhar Ali\AppData\Local\Programs\Python\Python36\python.exe
Python Version:     3.6.6
Python Path:    

['E:\\Web\\web dev\\Practice-code\\mysite',
 'C:\\Users\\Mazhar '
 'Ali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
 'C:\\Users\\Mazhar Ali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
 'C:\\Users\\Mazhar Ali\\AppData\\Local\\Programs\\Python\\Python36\\lib',
 'C:\\Users\\Mazhar Ali\\AppData\\Local\\Programs\\Python\\Python36',
 'C:\\Users\\Mazhar '
 'Ali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']

Server time:    Sat, 15 Sep 2018 08:15:26 +0000
Traceback Switch to copy-and-paste view

    C:\Users\Mazhar Ali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py in inner

                    response = get_response(request)

         ...
    ▶ Local vars
    C:\Users\Mazhar Ali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py in _get_response

                        response = self.process_exception_by_middleware(e, request)

         ...
    ▶ Local vars
    C:\Users\Mazhar Ali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py in _get_response

                        response = wrapped_callback(request, *callback_args, **callback_kwargs)

         ...
    ▶ Local vars
    E:\Web\web dev\Practice-code\mysite\flights\views.py in login

                    login(user)

         ...
    ▶ Local vars
    E:\Web\web dev\Practice-code\mysite\flights\views.py in login

            if request.method=='POST':

         ...
    ▶ Local vars

Request information
USER

mazharali
GET

No GET data
POST
Variable    Value
csrfmiddlewaretoken     

'APmMk0KHsGbdk53l1bgRGCCSaub9OVauHT6ZQvUmK5SOBy9hHcJcEHcHVjPkUs3z'

name    

'Mazhar'

password    

'ali'

FILES

No FILES data
COOKIES
Variable    Value
csrftoken   

'zThgQxnzqEMymKDx703M2QihSTSW0r3YGX1tm2xeI3t9DdJtN1w70VS6DIw76YW3'

sessionid   

'gyzu709und6s74mt5c6v1df499570rmp'
python django web django-users
1个回答
3
投票

您可以通过函数login覆盖django登录方法。只需将 def login(request):重命名为其他内容,例如 def view_login(request):,并且不要忘记更改导入和网址。

© www.soinside.com 2019 - 2024. All rights reserved.