Learning Django by Example(4): First user authenticated
Python, Web September 18th, 2007
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:
>>> 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:
add the URL mapping in urls.py:
(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:
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:
Howdy, {{ user.username }} [Logout, My Profile]
{% else %}
[Login or Register]
{% endif %}
as the following(left: logged-in user, right: logged-out user):
Check out r25 for the implementation details.







[...] 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 [...]
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?