Last Updated: February 25, 2016
·
373
· josetomastocino

Customising Django's auth views

The official Django tutorial does not always show the best practices when dealing with proper code separation. For instance, in several parts of the documentation they place business logic in the urls.py routes file. Examples here and here. That's a no-no.

In python, explicity is better than implicit, so rather than sticking important information in a routes files, just create a simple view wrapper to build and pass the necessary parameters to the view. For instance, when dealing with the auth views, you can either use traditional functions or even a lambda -- anything, as long as the logic stays in the views file.

# First option
def password_reset(request):
    context = {}
    context["form_submit"] = _("Send reset code")
    context["form_class"] = "narrow"

    return auth_views.password_reset(
        request=request,
        template_name="generic_form.html",
        extra_context=context
    )

# Something more hacky
password_reset_done = lambda request: auth_views.password_reset_done(
    request=request,
    template_name="auth/password_reset_done.html"
)