I am just playing around with the Django login and logout phase. I have a login page, if the user gets it with 'GET', he gets the form, if he submits the form, it gets submitted to the same view.login
function. The problem is that when i try to use ' if request.method=='POST'
. I get an error that 'User has not attribute method'
, i don't understand, why does the 'User'
object gets passed instead of the request
object?
Here's the code of 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")
Here's the login page:
<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
Here's the error message (Not a full message):
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'
You override the django login method by your function login
. Just rename the
def login(request):
to something else for example
def view_login(request):
and don't forget to change imports and urls.