Sunday, March 22, 2015

Chapter 12 - User Authentication with Django Registration Redux

A couple notes for this chapter. In 12.1, make sure you are updating the correct urls.py file. There are two - one is: tango_with_django_project/tango_with_django_project/urls.py, while the other is in tango_with_django_project/rango/urls.py.

You want to update the first one with (r'^accounts/', include('registration.backends.simple.urls')),


In 12.3.6 you are asked to update the base.html template to account for the new links shown on the page http://127.0.0.1:8000/accounts/ (shown above). Don't panic if you see a weird looking 404 error page. Just look at the links listed and see why it makes sense that the tutorial is asking you to replace 'register' with 'registration_register', and 'login' with 'auth_login', etc.

Also important to read this sentence: Notice that for the logout, we have included a ?next=/rango/. This is so when the user logs out, it will redirect them to the index page of rango. If we exclude it, then they will be directed to the log out page (but that would not be very nice). 

The sentence refers to this direction: 
  • logout to point to <a href="{% url 'auth_logout' %}?next=/rango/">
In the future add "?next=<link path here>" to the end of a link if you want to redirect somewhere else.

However, to redirect a user to a custom page after a password has been changed, create a file called password_change_done.html and save it in templates/registration. See this documentation page for more details. You can base it off your base.html template and make it something like:

{% extends "rango/base.html" %}

{% block body_block %}
<h1>Password Changed</h1>
        <p>Good news! Your password has been changed.</p>
{% endblock %}

At first, I attempted to achieve this by adding a function in views.py that redirected to a template file in the regular templates/rango folder. The correct way is create a custom password_change_done.html file and save it in the registration template folder so that it shows you that page instead of the default page shown here:

This is a problematic page. Sure, it tells you that your password change was successful, but what if, after changing my password, I want to continue to browse the site? There's no link to the homepage. Clicking on "home" takes you to an admin login page, so if you're not an administrator, you've reached a dead end. It makes much more sense to direct the user to a custom page that tells the user that his/her password has been changed successfully, along with a link to the homepage.

This took me a long time to figure out, but I'm glad I managed to cobble together a solution. Phew. Onto the next chapter...

No comments:

Post a Comment