gpt4 book ai didi

django - Django 2.0 问题 : 'WSGIRequest' object has no attribute 'session'

转载 作者:行者123 更新时间:2023-12-03 01:30:43 25 4
gpt4 key购买 nike

我将 Django 版本从 1.11.5 升级到 2.0,并且我正在尝试解决不同的已弃用元素。

但是,即使我的 CSS/bootstrap 样式表不起作用,我也无法登录到我的 Django 软件。我有这个问题:

'WSGIRequest' object has no attribute 'session'

这是整个回溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/Authentification/Login/

Django Version: 2.0
Python Version: 3.6.2
Installed Applications:
['Institution',
'django.conf.urls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrapform',
'django_countries',
'chartit',
'Configurations',
'Home',
'Authentication',
'Identity',
'rest_framework',
'Fiscal',
'bootstrap4']
Installed Middleware:
[]



Traceback:

File "/Users/valentinjungbluth/Desktop/DatasystemsCORE3.6/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)

File "/Users/valentinjungbluth/Desktop/DatasystemsCORE3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)

File "/Users/valentinjungbluth/Desktop/DatasystemsCORE3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/valentinjungbluth/Desktop/DatasystemsCORE3.6/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)

File "/Users/valentinjungbluth/Desktop/Django/DatasystemsCORE/DatasystemsCore/DatasystemsCORE/Authentication/views.py" in Login
26. login(request, user)

File "/Users/valentinjungbluth/Desktop/DatasystemsCORE3.6/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in login
130. if SESSION_KEY in request.session:

Exception Type: AttributeError at /Authentification/Login/
Exception Value: 'WSGIRequest' object has no attribute 'session'

在我的身份验证应用程序中:

# views.py file
#-*- coding: utf-8 -*-

from django.contrib.auth import authenticate, login, logout
from .forms import ConnexionForm
from django.shortcuts import render, reverse, get_object_or_404, redirect
from django.http import HttpResponseRedirect, HttpResponse
from .models import LoggedUsers

from API_GED import Logger

import datetime
from django.views.decorators.csrf import csrf_exempt,csrf_protect


@csrf_exempt
def Login(request):
error = False

if request.method == "POST":
form = ConnexionForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
user = authenticate(username=username, password=password) # Nous vérifions si les données sont correctes
if user: # Si l'objet renvoyé n'est pas None
login(request, user)

response = redirect('Homepage')

return response

else: # sinon une erreur sera affichée
error = True

else:
form = ConnexionForm()

return render(request, 'Authentication_Homepage.html', locals())


def Logout(request):

logout(request)

return redirect(reverse('Choice'))


def ConnectedUsers(request) :

logged_users = LoggedUsers.objects.all()
print (logged_users)

logged_users_number = LoggedUsers.objects.all().count()

context = {
"logged_users":logged_users,
"logged_users_number":logged_users_number,
}

return render(request, "Authentication_LoggedUsers.html", context)

我的 models.py 文件:

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in, user_logged_out

# class UserProfile(models.Model):
# user = models.OneToOneField(User)
# avatar = models.ImageField(upload_to='/images/')

class LoggedUsers(models.Model):
user = models.ForeignKey(User, primary_key=True, on_delete=models.CASCADE)

def __str__(self):
return '{}{}{}'.format(self.user.first_name + " ", self.user.last_name + " (", self.user.username + ")")

def login_user(sender, request, user, **kwargs):
LoggedUsers(user=user).save()

def logout_user(sender, request, user, **kwargs):
try:
u = LoggedUsers.objects.get(user=user)
u.delete()
except LoggedUsers.DoesNotExist:
pass

user_logged_in.connect(login_user)
user_logged_out.connect(logout_user)

在我的 settings.py 文件中,我有 MIDDLEWARE_CLASSES :

MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'DatasystemsCORE.middleware.OnlineNowMiddleware',
]

有什么想法吗?

编辑:

我这样写了MIDDLEWARE:

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

看起来有效!

最佳答案

关于django - Django 2.0 问题 : 'WSGIRequest' object has no attribute 'session' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com