prototype
prototype

Reputation: 3313

Django user authentication not working

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..

Upvotes: 1

Views: 1724

Answers (2)

user956424
user956424

Reputation: 1609

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

Upvotes: 1

prototype
prototype

Reputation: 3313

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.

Upvotes: 6

Related Questions