Skip to content Skip to sidebar Skip to footer

Request.user.is_authenticated Is False On Certain Html On Certain Pages But Not In Views

Most pages on this website I'm creating for practice {% if request.user.is_authenticated %} go through as true. But there is one page that always returns this as false (I've pasted

Solution 1:

When you create a django project there is such a thing as template context processor, context processor makes available variables, that we create in our code in templates.

'django.contrib.auth.context_processors.auth', sets the request user variable, and makes it available in every django template.

In your code you have overwritten django user variable with a string variable currentUser, the currentUser string does not have is_authenticated property, hence you get False.

Solution 2:

user object is exisiting in global context. You overriden the user object with a text string.

currentUser = str(request.user)

"user": currentUser

It is not user object anymore. It is just a simple string. Thats why you got the confusion.

Solution 3:

You can use decorators in your views for deny any unauthenticated user. You don't need to be use {% if request.user.is_authenticated %} in your html. just add decorators in your views.

from django.contrib.auth.decorators import login_required

@login_required  deflisting(request, name, num='0'):
    .........

Post a Comment for "Request.user.is_authenticated Is False On Certain Html On Certain Pages But Not In Views"