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
- 1. Introduction
- 2. Installation and setup
- 3. How things work
- 4. The first Django app
- 5. Your first view
- 6. Your first template
- 7. Testing our site
- 8. Extending templates
- 9. Your first model
- 10. The admin interface
- 11. Showing contents from the model
- 12. Another app (category)
- 13. Connecting two models
- 14. Show list of categories
- 15. Category detail page
- 16. Separate url files and why
- 17. Adding tasks in the front end
- 18. Editing tasks
- 19. Completing and deleting tasks
- 21. Prettying up the design a little bit
- 22. Make it possible to sign up
- 23. Logging in
- 24. Logging out