Implementing Login/Logout in Django

Update: add names and namespace to URLs

Implementing user authentication is fairly easy job in Django, many functionalities are already included in the standard Django installation, you can manage users using the default “admin” app the comes with Django.

Here I will show how implement Login/Logout feature by relying on Django built-in views.

Steps:

  • Create “accounts” app: we will put all “accounts” related files in this app.
  • Create URL mapping for login and logout pages.
  • Include “accounts” URL in the main URL mapping.
  • Create login page HTML template
  • Add small template code to show current user in all pages (changing base template)

# Create "accounts" app
$ ./manage.py startapp accounts
# Add to INSTALLED_APPS
$ vi project_name/settings.py 
INSTALLED_APPS = (
  :
  'accounts',
  :
)


# Create URL mapping for login and logout pages
$ vi accounts/urls.py 
from django.conf.urls import patterns, url
urlpatterns = patterns('',
    url(
        r'^login/$',
        'django.contrib.auth.views.login',
        name='login',
        kwargs={'template_name': 'accounts/login.html'}
    ),
    url(
        r'^logout/$',
        'django.contrib.auth.views.logout',
        name='logout',
        kwargs={'next_page': '/'}
    ),
)

# Include "accounts" URL in the main URL mapping
$ vi project_name/urls.py 
urlpatterns = patterns('',
    :
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    :
)


# Create login page HTML template
$ vi templates/accounts/login.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'accounts:login' %}">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="/" />
</form>
{% endblock %}


# Add small template code to show current user
$ vi template/base.html
:
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}  <a href="{% url 'accounts:logout' %}">[logout]</a></p>
{% else %}
    <p>Welcome, Guest <a href="{% url 'accounts:login' %}">[login]</a></p>
{% endif %}
:

For more on the subject:
Using the Django authentication system

Bonus: Password Change form

$ vi accounts/urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('',
    :
    url(
        r'^password_change$',
        'django.contrib.auth.views.password_change',
        name='password_change',
        kwargs={
               'template_name': 'accounts/password_change_form.html',
               'post_change_redirect':'accounts:password_change_done',
               }
    ),
    url(
        r'^password_change_done$',
        'django.contrib.auth.views.password_change_done',
        name='password_change_done',
        kwargs={'template_name': 'accounts/password_change_done.html'}
    ),
    :
)


$ vi templates/accounts/password_change_form.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Password change failed</p>
{% endif %}
<form method="post" action="{% url 'accounts:password_change' %}">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Change Password" />
</form>
{% endblock %}


$ vi templates/accounts/password_change_done.html
{% extends "base.html" %}
{% block content %}
Password successfully changed.
<a href="/">Home</a>
{% endblock %}


$ vi template/base.html
:
{% if user.is_authenticated %}
    <p>
    Welcome, {{ user.username }}
    <a href="{% url 'accounts:password_change' %}">[Change Password]</a>
    <a href="{% url 'accounts:logout' %}">[logout]</a>
    </p>
{% else %}
    <p>Welcome, Guest <a href="{% url 'accounts:login' %}">[login]</a></p>
{% endif %}
:

8 thoughts on “Implementing Login/Logout in Django”

  1. Thanks a lot! I did not know you can overwrite ‘post_change_redirect’ and it gave me so much headache because of namespacing issues.

  2. @Umut Glad you like, please do translate it.
    May I ask what language would it? I am guessing Turkish!
    and please send me the link, I link to it if you don’t mind.

  3. Thanks so much. I was implementing something like this last night and just realized I did a foolish mistake. I did not include my accounts URL in my main. Actually I was working on main url file and got some module error. It felt like searching a spade instead of a spoon. …

Leave a Reply

Your email address will not be published. Required fields are marked *