In the last post, we succeeded to update the database, in this post we would add more functionalities to the view of normal users.

In r23, I re-organize the file directory in the repository, media is added to the repository for your convenience.

Though Gelman is designed for personal use, the multi-user functionality is added just in case you want to share you collection with family members and friends later.The built-in authentication is quite neat, if you deploy Gelman into an enterprise environment, you may consider this. Now, just follow the documentation, and add the very first reader to Gelman by running python manage.py shell:

>>> from django.contrib.auth.models import User
>>> u = User.objects.create_user(‘python’, , ’spam’)
>>> u.save()

Now, let’s build the gateway for the user to login. Copy the basic login form in the doc to templates/login.html, The default success login action is to redirect to user’s profile, we may override it to the index first:

<input type="hidden" name="next" value="/library/" />

add the URL mapping in urls.py:

(r‘^accounts/login/$’, ‘django.contrib.auth.views.login’, {‘template_name’: ‘login.html’, }),
       (r‘^accounts/logout/$’, ‘gelman.library.views.logout_view’, ),

For logout action, just logout the user and redirect the user to the front page, in library/views.py:

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(‘/library’)

We would greet the users in the top-left corner of the page just as Wordpress’s dashboard does. Add the following code snippet to base_generic.html:

{% if user.is_authenticated %}
Howdy, {{ user.username }} [Logout, My Profile]
{% else %}
[Login or Register]
{% endif %}

as the following(left: logged-in user, right: logged-out user):

Login and Logout demo



Check out r25 for the implementation details.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

2 Comments to “Learning Django by Example(4): First user authenticated”

  1. Learning Django by Example(5): Time to Attack by Refactor the Life | September 19th, 2007 at 2:24 pm

    [...] Time to Attack Posted on September 19th, 2007 in Gentoo In the last four section(1, 2, 3, 4) ,we have been familiar with the environment, it is time to do some real [...]

  2. abel.bolanosNo Gravatar | September 19th, 2008 at 5:16 am

    Hi, i got a question for you.
    How you show the ‘error_message’ in the template ‘login.html’ if the login was wrong.
    Because i can’t show that. Admin login works fine but mine don’t show errors_message. I want to know WHY?

Leave a Comment

This site is using OpenAvatar based on