As you used the login()
function from the django auth
module, to login a specific user, similarly you can use the logout()
fucntion from the django auth
module to logout a user from the website.
Create a function for logout in your authentication/views.py
file. This function logouts the user and redirects
him to a particular page (register page).
from django.contrib.auth import logout
def user_logout(request):
logout(request)
return redirect('register')
A route needs to be created for this function which can be done as follows by editing the urlpatterns
inside the authentication/urls.py
file:
path("logout", views.user_logout, name='logout'),
After this, whenever the http://127.0.0.1:8000/logout url is visited it will logout the user from the website.
Lets add the logout
button inside the home.html
file.
<a href="{% url 'logout' %}">Logout</a>
Inside the single quotes
the specfic name
which is given inside the urlpatterns
should be there.
After these changes, the webpage would look like this :
On clicking the logout
button the user would be redirected to the register
page and he would need to login
to visit this page again.