Mike IT Mike IT - 16 days ago 4
Python Question

Django 1.7 AttributeError: 'WSGIRequest' object has no attribute 'user'

I have read some similar posts but none helped me plus I was recommended to create a new question.

I try to reach to admin page of an inherited web-site and I created a user for that matter but I reach to the admin page I get the below stack trace. I have also added the settings.py in case it helps.

Any input is much appreciate it.

M

settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXX'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# ALLOWED_HOSTS = ['0.0.0.0', 'localhost']
ALLOWED_HOSTS = ['192.168.1.36', 'localhost']


# Application definition

INSTALLED_APPS = [

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 'south',
'CordeliaHanelBackend',
'tastypie',
'StudioHanel',
]

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',
]


ROOT_URLCONF = 'CordeliaHanelBackend.urls'

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
'/var/www/CordeliaHanelBackend-master/StudioHanel/templates2',
)

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': ['/var/www/CordeliaHanelBackend-master/StudioHanel/templates2'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'CordeliaHanelBackend.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}

}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
# os.path.join(BASE_DIR, "static"),
# '/var/www/CordeliaHanelBackend-master/StudioHanel/templates2',
'/var/www/CordeliaHanelFrontend-master/www/dist/',
# '/var/www/CordeliaHanelFrontend-master/www/app',
# '/var/www/CordeliaHanelBackend-master/StudioHanel/static',
)

# STATIC_ROOT = '/var/www/CordeliaHanelFrontend-master/www/app'


STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# DEFAULT_FILE_STORAGE = '/var/www/CordeliaHanelFrontend-master/www/app'


SOUTH_DATABASE_ADAPTER = {
'default': 'south.db.sqlite3'
}


Stack-trace / Error log

[Wed Nov 16 20:50:07.826886 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] Internal Server Error: /admin/
[Wed Nov 16 20:50:07.826923 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] Traceback (most recent call last):
[Wed Nov 16 20:50:07.826927 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
[Wed Nov 16 20:50:07.826930 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Wed Nov 16 20:50:07.826932 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 225, in wrapper
[Wed Nov 16 20:50:07.826934 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] return self.admin_view(view, cacheable)(*args, **kwargs)
[Wed Nov 16 20:50:07.826937 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
[Wed Nov 16 20:50:07.826939 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] response = view_func(request, *args, **kwargs)
[Wed Nov 16 20:50:07.826941 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
[Wed Nov 16 20:50:07.826944 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] response = view_func(request, *args, **kwargs)
[Wed Nov 16 20:50:07.826946 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 193, in inner
[Wed Nov 16 20:50:07.826948 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] if not self.has_permission(request):
[Wed Nov 16 20:50:07.826951 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 150, in has_permission
[Wed Nov 16 20:50:07.826953 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] return request.user.is_active and request.user.is_staff
[Wed Nov 16 20:50:07.826955 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] AttributeError: 'WSGIRequest' object has no attribute 'user'


WSGI file

import os

master/env/lib/python2.7/site-packages/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CordeliaHanelBackend.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Answer

I had the same problem and I fixed by:

1) changing MIDDLEWARE to MIDDLEWARE_CLASSES 2) commenting out SecurityMiddleware

like below:

MIDDLEWARE_CLASSES = [
    # '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',
]
Comments