'WSGIRequest' object has no attribute 'is_authenticated'

1.9k views Asked by At

I am checking whether the user is authenticated or not in my views.py. Django however keeps throwing exception WSGIRequest object has no attribute 'is_authenticated'. I read through some of the similar questions on both stackoverflow and github regarding this issue, everyone was pointing towards moving the middleware.I have tried almost every way to restructure my MIDDLEWARE as mentioned in other answers for the same issue but I haven't been able to resolve it. I even read the documentation for ordering middleware but wasn't able to gather much from this.

views.py

@staticmethod
def cmshome(request):
    if request.is_authenticated:
        return redirect('/cms/')
    username = request.session['user']
    profile_object = Profile.objects.all().filter(user__username=username)
    return render(request, 'cms/cmshome.html', {'profile_content': profile_object})

Middleware

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

There are 1 answers

0
Vladyslav Oliinyk On BEST ANSWER

Maybe you wanted to write:

if request.user.is_authenticated:
    return redirect('/cms/')