简体   繁体   中英

'WSGIRequest' object has no attribute 'user'

I, for the life of me, can't get this to work. I've researched what this error means and have really only received a response close to "change MIDDLEWARE to MIDDLEWARE_CLASSES". This did not work. I've tried rearranging Middleware Classes, however, this too did not work. Is there anything in my code i should be concerned with that is causing this error?

Methodology:

When a user logs in, the user is directed to .com/user/ which will determine which dashboard to display based on the is_userA or is_userB attribute. However, when I log in, im presented with the 'WSGIRequest' object has no attribute 'user'.

traceback

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()
 Exception Value: 'WSGIRequest' object has no attribute 'User' 

那是因为属性是“用户”。

为用户使用小写字母:

request.user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM