1

I'm using the default model User from django (since I'm using django-facebook) and the user authentication does not work in the template. Here is the code:

{% if request.user.is_authenticated %}
   Welcome, {{ request.user.first_name }}
   <a href="/logout">logout</a>
{% else %}
   <form action="{% url facebook_connect %}?facebook_login=1" method="post">
        <a href="javascript:void(0);" style="font-size: 20px;" onclick="F.connect(this.parentNode);">
             <img src="/media/images/site/facebook_login.png" alt="facebook_login" class="facebook_login_button"/>
        </a>
        <input type="hidden" value="{{ request.path }}" name="next" />
   </form>
{% endif %}

I've passed the context_instance from the view:

return render_to_response('index.html', context, context_instance=RequestContext(request))

But still it won't work. Thanks for your help!

EDIT:

The user successfully logs in with django-facebook (inserted into auth_user table) and his data is shown but when the user logs out his data disappears and the login form as seen on the edited code above is not show!

Here is the complete view I use for logging in and out the user:

def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')

def signin(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            # TO RETURN ERROR
            return HttpResponseRedirect('/')
    else:
        # TO RETURN ERROR
        return HttpResponseRedirect('/')

If I remove the request from request.user.first_name an error appears:

You need to set AUTH_PROFILE_MODULE in your project settings

That is about all the details I can get..

3
  • 1
    Your question is too vague. Is there an error returned? Is your user signed in but it the welcome message not shown? What exactly does-not-work...? Commented Oct 10, 2012 at 22:38
  • So you are saying that the template always ends up rendering "Welcome, <name>" ? Commented Oct 10, 2012 at 23:00
  • Well at first when the user logs in it says his name, but when he logs out it stays blank. Commented Oct 10, 2012 at 23:14

2 Answers 2

6

I am not aware of the reason why request.user.is_authenticated is not working but I have found a solution - Using

{% if not user.is_anonymous %}

instead of

{% if request.user.is_authenticated %}

seems to do the trick. The if works and only logged in users are shown their info and the log out button.

Sign up to request clarification or add additional context in comments.

3 Comments

Did you ever figure out why? I had the same result - this worked great for me, but I don't know why the former didn't work.
Sorry Christopher, I was moved from that project pretty soon after I posted this and didn't had the time to figure out why it wasn't working properly.
I think what may be happening is that the user object is not actually defined / in context, so user.is_anonymous returns false because the object is not in scope - hence the if not returns true and the branch is executed. If I'm right, this may actually be quite a slippery slope. But I'm not sure! @ChristopherArmstrong, what do you think?
1

is_authenticated() is a function call and not an attribute that can be used as you have done in the template code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.