Why can't I logout on django user auth?

Derek picture Derek · Jun 14, 2011 · Viewed 14.8k times · Source

I am using the django.contrib.auth user management system.

So I got the registration/insert into the user table/model up and the login from django.contrib.auth.views.login up so I can log in.

However, I can't use django.contrib.auth.views.logout to logout

I have in my template

<h1>My Account</h1>
<strong> Welcome, {{ name|capfirst }}!</strong>
<br /><br />
<ul>
    <li>
        {% if user.is_authenticated %}
            <a href="{% url django.contrib.auth.views.logout %}">Logout</a>
        {% else %}
            <a href="{% url register %}">Sign Up</a>
    </li>
    <li>
            <a href="{% url django.contrib.auth.views.login %}">Login</a>
        {% endif %}
    </li>
</ul>

However I always get the name and the logout link because I never actually logout when I click on the logout button

Here is my urls.py section for this:

urlpatterns += patterns('django.contrib.auth.views',
    url(r'^login/$', 'login', { 'template_name': 'registration/login.html', 'SSL': settings.ENABLE_SSL }, 'login' ),
    url(r'^my_account/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),
)

What am I doing wrong? Note: I am also running django via apache2 with mod_wsgi

Thanks!

Added Info:

Not sure if this helps but I printed request.session.items in the html and got

[('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 9L)] 

when I was logged in and also after I clicked the logout button (django.contrib.auth.views.logout)

Also, I created:

from django.contrib.auth import logout
def logout_view(request):
    request.session.items = []
    request.session.modified = True
    logout(request)

And linked that to a second logout link/button and I didn't logout and the request.session.items stayed the same as above after clicking the link

I think I'm closing in:

In one of my view functions I did:

request.session["fav_color"] = "blue"
request.session.modified = True

and then print in html {{ request.session.items }} which gave me

    [('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 9L)] 

and no ('fav_color', 'blue') tuple. Did I do something wrong again, or is this proof that my request.session list isn't being modified?

K figured it out:

url(r'^my_account/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),

should be

url(r'^logout/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),

Answer

mrbox picture mrbox · Jun 14, 2011

I think that urls.py could be like this(login and logout views do not accept SSL parameter):

from django.core.urlresolvers import reverse
urlpatterns += patterns('django.contrib.auth.views',
        url(r'^login/$', 'login', { 'template_name': 'registration/login.html'}, name='login' ),
        url(r'^logout/$', 'logout', { 'template_name': 'registration/my_account.html', 'next_page':reverse('index') }, name='logout' ),
)

And in the template:

<h1>My Account</h1>
<strong> Welcome, {{ name|capfirst }}!</strong>
<br /><br />
<ul>
    <li>
        {% if user.is_authenticated %}
            <a href="{% url logout %}">Logout</a>
        {% else %}
            <a href="{% url register %}">Sign Up</a>
    </li>
    <li>
            <a href="{% url login %}">Login</a>
        {% endif %}
    </li>
</ul>