Logging out - 30 days of Django

Logging out - 30 days of Django

/ #Django


In this part we will make it possible to log out, plus do some other configurations to the project.

Django logout function

Let's begin by editing the userprofile/urls.py file. We need to add the path to the logout page here:

from django.contrib.auth import views as auth_views
from django.urls import path

from . import views

urlpatterns = [
    path('sign-up/', views.signup, name='signup'),
    path('log-in/', auth_views.LoginView.as_view(template_name='userprofile/login.html'), name='login'),
    path('log-out/', auth_views.LogoutView.as_view(), name='logout'), # New
]

And there you have it, we can now log out by using Django's built in function for this. Let's link to this in the base.html file before we continue.

...

<nav class="navbar is-dark">
    <div class="navbar-brand">
        <a href="/" class="navbar-item is-size-4">Toodoo</a>
    </div>

    <div class="navbar-menu">
        <div class="navbar-end">
            {% if request.user.is_authenticated %}
                <a href="{% url 'logout' %}" class="navbar-item">Log out</a> 
            {% else %}
                <a href="{% url 'signup' %}" class="navbar-item">Sign up</a>
                <a href="{% url 'login' %}class="navbar-item">Log in</a>
            {% endif %}
        </div>
    </div>
</nav>

...

If you log out now, you will be redirected to a page that doesn't exist or that looks like a part of the admin interface. Let's reconfigure this, so we can control where the user is redirected to.

Open up settings.py, and add the three lines somewhere (It does not matter where):

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'frontpage'
LOGOUT_REDIRECT_URL = 'login'

LOGIN_URL: This is the page you are redirected to if you go to a page that you're not authorized to see.
LOGIN_REDIRECT_URL: This is the page you are redirected to after you log in.
LOGOUT_REDIRECT_URL: This is the page you are redirected to after you log out.

Guarding views / paths

Right now if you go to the front page, you will get an error because you're not logged in. We need to change this a little bit. We don't want an error.

We want to tell Django that you need to be authenticated to view this page, and then Django will automatically redirect you if you're not logged in.

from django.contrib.auth.decorators import login_required # 1. New
from django.shortcuts import render

...

@login_required # 2. New
def frontpage(request):
    ...

1. First we import something called a decorator. A decorator adds "configurations"/"rules" to a view.
2. Then we add the decorator above the view.

Add this decorator above all of the other views as well. Also in the category app. Just don't add them in the signup view :-)

Table of contents

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.