Django user.is_authenticated works some places, not others
Asked Answered
L

2

5

In my template, I have the following:

        <ul class="tabbed" id="network-tabs">
            {% if user.is_authenticated %}
            <li><a href="{% url acct-my-profile %}">My Account</a></li>
            <li><a href="{% url acct-logout %}">Log Out</a></li>
            {% else %}
            <li><a href="{% url acct-login %}">Log in</a></li>
            <li><a href="{% url acct-register %}">Register</a></li>
            {% endif %}
        </ul>

It seems to work fine, unless the page been created has a @login_required decorator, in which case the page works fine but the navigation appears as if the user is not logged in, even when they are.

Lemming answered 26/7, 2010 at 17:42 Comment(0)
W
14

You should check your view function to see where the user variable is coming from. Unless you're specifically passing user into the context from the view, that's your problem.

You do have access to request.user, though, and that will always return true in a template rendered from a view that has the @login_required decorator.

The reason I can tell you for certain that there's nothing wrong with the decorator, though, is that in the code for User and AnonymousUser (located in django.contrib.auth.models) the is_authenticated method strictly returns true for User and false for AnonymousUser. The decorator does not and cannot change that. And what that means is that your template isn't actually getting a User object where you're checking user.

Whisky answered 26/7, 2010 at 21:3 Comment(2)
Ah! Perfect. I wasn't passing the user through at all, just as you said. But for others (since it confused me), you can't use "request.user" in the template, you need to pass through the user in your render to response like: 'user' : request.user, Thanks again.Lemming
See Alasdair's answer for further information in response to your comment.Whisky
P
8

To follow on from Gabriel's answer, is the user variable coming from the auth context processor? If it is, and you are using the render_to_response shortcut, you need to use a RequestContext instance.

from django.template import RequestContext

...

@login_required
def some_view(request):
    # ...
    return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))
Phosphene answered 26/7, 2010 at 22:29 Comment(3)
Also an excellent point. I was about to add a note on context processors and RequestContext.Whisky
I am having this problem too: after login correctly, I redirect to home page, wich view returns render_to_response('myapp/shop_list.html', {'shop': entry_list,}, context_instance=RequestContext(request)) But, at this point, request.user is an AnonymusUser so when the home page template loads, user.is_authenticated is always false. What can I be missing?Naivete
It sounds like your login isn't working. If you add print request.user to your home page view, do you get the user you are expecting? If that doesn't help, please ask a new question instead of adding comments here -- not many people will read these comments, so you won't get any help.Phosphene

© 2022 - 2024 — McMap. All rights reserved.