对于我的生活,我无法让这一切发挥作用。我已经研究了这个错误意味着什么,并且实际上只收到了接近“将MIDDLEWARE改为MIDDLEWARE_CLASSES”的响应。这没用。我已经尝试重新安排中间件类,但是,这也没有用。我的代码中有什么我应该关注的是导致此错误吗?
方法
当用户登录时,用户将被定向到.com / user /,它将根据is_userA或is_userB属性确定要显示的仪表板。但是,当我登录时,我提供的'WSGIRequest'对象没有属性'user'。
回溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/user/
Django Version: 1.10.3
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.sites',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'allauth',
'allauth.account',
'allauth.socialaccount',
'userprofiles']
Installed 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']
Traceback:
File "/Users/*/Desktop/*env/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Users/*/Desktop/*env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/*/Desktop/*env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/*/Desktop/*env/*/userprofiles/views.py" in logged_in
5. if request.User.Profile.is_userA:
Exception Type: AttributeError at /user/
Exception Value: 'WSGIRequest' object has no attribute 'User'
views.py
from django.shortcuts import render
def logged_in(request):
if request.User.Profile.is_userA:
return render(request, "userA_dashboard.html")
if request.User.Profile.is_userB:
return render(request, "userB_dashboard.html")
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.logged_in, name='index')
]
models.py
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
#account types
is_userA = models.BooleanField('User A', default=True)
is_userB = models.BooleanField('User B', default=False)
is_superuser = models.BooleanField('SuperUser', default=False)
#other fields here
avatar = models.ImageField('avatar', upload_to='static/media/images/avatars/', null=True, blank=True)
phone = models.CharField('phone number', max_length=20, blank=True, default='')
address = models.CharField('address', max_length=100, default='', blank=True)
city = models.CharField('city', max_length=100, default='', blank=True)
state = models.CharField('state', max_length=2, default='', blank=True)
country = models.CharField('country', max_length=100, default='', blank=True)
date_joined = models.DateTimeField(default=timezone.now)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
答案 0 :(得分:3)
Exception Value: 'WSGIRequest' object has no attribute 'User'
那是因为该属性是“用户”。
答案 1 :(得分:2)
为用户使用小写:
request.user