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 settings.py 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:

'django.middleware.csrf.CsrfViewMiddleware',

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

(r'^login/$','django.contrib.auth.views.login'),

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: http://jordanmessina.com/2010/05/24/django-1-2-csrf-verification-failed/
Otherwise, you get a nice 403 error telling you it’s missing.

Now you can go to views.py 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 “forms.py” in the bookmarks application folder.
Here are the entire contents:

Next you’ll need to update the views.py:

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 urls.py:

(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


Name*

Email(will not be published)*

Website

Your comment*

Submit Comment

© Copyright Duke Hall - Designed by Pexeto