Django & Python Adventure Part 4

In this post we’ll be dealing with Django’s User Authentication and Base Templates (Master Pages). These are things every project requires and so you cannot really know a platform until you’ve covered this ground.

First let’s take care of the Base Template. In this part we’ll add a special kind of template that is used by other templates. If you’re a .Net person, this is a Master Page. If you’re some other kind of person…. Hiya! We’ll also add a style sheet to our project to handle a universal nav:
In your project, add the following pieces:
project folder

So, a folder called “site_media” with a style.css in it and in the templates folder a base.html.
styles.css will look like this:

We’ve already included the auth library in the under the INSTALLED_APPS section, but we have to modify the MIDDLEWARE_CLASSES section to allow our sessions to save a cookie.
Find that section and add this line to the list:


Now we can jump to the and setup a line to instantiate the authentication login view:


Now add a folder to your templates directory called “registration” and add a page to that called “login.html” and paste in this code:

IMPORTANT! That {% csrf_token %} was not in the book and took some research to figure out. Ultimately, it was this blog that got me past that bug:
Otherwise, you get a nice 403 error telling you it’s missing.

Now you can go to and add in the following and replace your main_page definition:

And redo the user_page function in the same page to use the render_to_reponse library:

Now we’ll add a page “” in the bookmarks application folder.
Here are the entire contents:

Next you’ll need to update the

It will come out like this:
registration page

Now we need to modify the top navigation to account for these new options, so edit your base.html nav id:

Lastly we’ll implement a successful message page for the registration process. If you’ve noticed in this walk through Django provides a lot of functionality for you out of the box. Here is another example. There is a view inside the django.views.generic.simple library called direct_to_template.

Create a register_success.html inside your templates/registration folder and enter this code in:

Then update your

(r'^register/success/$', direct_to_template, {'template':'registration/register_success.html'})

Tmie to test this out:
registration form tested
Hit register and:
registration success
(PS-if you’re paying attention you’ll notice the nav at the tops say’s “sa” and not “ishmael”. I had to logout of sa and do it again to get that to work right, but my post-production budget is tight)

Afterthoughts: This took a bit more time for me to debug, and there’s probably some libraries that have changed since the book I’m reading. I noticed that PyCharm gave me a deprecation warning on this library: django.views.generic.simple, so that’s something I’ll check into later.

Overall though, I’m impressed at how simple it was to add Authentication. Although this is something you do once per project, I’ve seen a lot of custom code that makes it extremely painful. For me, saving lines of code means simplicity which is what I’m going for. If you have any questions or comments, feel free to shoot them over.

Leave a comment


Email(will not be published)*


Your comment*

Submit Comment

© Copyright Duke Hall - Designed by Pexeto