Homework: Adding security to your website

    First let’s make things secure. We will protect our , post_edit, post_draft_list, post_remove and post_publish views so that only logged-in users can access them. Django ships with some nice helpers for doing that, called decorators. Don’t worry about the technicalities now; you can read up on these later. The decorator we want to use is shipped in Django in the module django.contrib.auth.decorators and is called login_required.

    So edit your blog/views.py and add these lines at the top along with the rest of the imports:

    Then add a line before each of the post_new, post_edit, post_draft_list, post_remove and post_publish views (decorating them) like the following:

    1. @login_required
    2. def post_new(request):
    3. [...]

    That’s it! Now try to access http://localhost:8000/post/new/. Notice the difference?

    You should get one of our beloved errors. This one is quite interesting, actually: the decorator we added will redirect you to the login page, but since that’s not yet available, it raises a “Page not found (404)”.

    Don’t forget to add the decorator from above to post_edit, post_remove, post_draft_list and too.

    We could now try to do lots of magical stuff to implement users and passwords and authentication, but doing this correctly is rather complicated. As Django is “batteries included”, someone has done the hard work for us, so we will make further use of the authentication tools provided.

    In your mysite/urls.py add a url url(r'^accounts/login/$', views.login, name='login'). So the file should now look similar to this:

    1. from django.conf.urls import include, url
    2. from django.contrib import admin
    3. from django.contrib.auth import views
    4. urlpatterns = [
    5. url(r'^accounts/login/$', views.login, name='login'),
    6. url(r'', include('blog.urls')),
    7. ]

    Then we need a template for the login page, so create a directory blog/templates/registration and a file inside named login.html:

    You will see that this also makes use of our base template for the overall look and feel of your blog.

    The nice thing here is that this just worksTM. We don’t have to deal with handling of the form submission nor with passwords and securing them. Only more thing is left to do. We should add a setting to mysite/settings.py:

    1. LOGIN_REDIRECT_URL = '/'

    so that when the login page is accessed directly, it will redirect a successful login to the top-level index (the homepage of our blog).

    We already set things up so that only authorized users (i.e. us) see the buttons for adding and editing posts. Now we want to make sure a login button appears for everybody else.

    1. <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>

    For this we need to edit the templates, so let’s open up blog/templates/blog/base.html and change it so the part between the <body> tags looks like this:

    You might recognize the pattern here. There is an if-condition in the template that checks for authenticated users to show the add and edit buttons. Otherwise it shows a login button.

    Let’s add some sugar to our templates while we’re at it. First we will add some details to show when we are logged in. Edit blog/templates/blog/base.html like this:

    1. <div class="page-header">
    2. <a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
    3. <a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
    4. <p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>
    5. {% else %}
    6. <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
    7. {% endif %}
    8. </div>

    This adds a nice “Hello <username>“ to remind us who we are logged in as, and that we are authenticated. Also, this adds a link to log out of the blog — but as you might notice this isn’t working yet. Let’s fix it!

    We decided to rely on Django to handle login, so let’s see if Django can also handle logout for us. Check and see if you find something.

    Done reading? By now you may be thinking about adding a URL in mysite/urls.py pointing to Django’s logout view (i.e. django.contrib.auth.views.logout), like this:

    1. from django.conf.urls import include, url
    2. from django.contrib import admin
    3. from django.contrib.auth import views
    4. urlpatterns = [
    5. url(r'^admin/', admin.site.urls),
    6. url(r'^accounts/login/$', views.login, name='login'),
    7. url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
    8. url(r'', include('blog.urls')),
    9. ]

    That’s it! If you followed all of the above up to this point (and did the homework), you now have a blog where you

    • need a username and password to log in,
    • and can log out again.