We will continue to build our blog app where we left in the post writing-your-first-blog-app-in-django-part-15.
So upto now we have a app that login the user into our application.In this post we will see how to log those users out the application.The logout part in django is also pretty simple as login part.
So to add logout section first go to the accounts/urls.py file and add a url for logout as following:
from django.conf.urls import url from . import views app_name = 'accounts' urlpatterns = [ url(r'^signup/$', views.signup_view, name="signup"), url(r'^login/$', views.login_view, name="login"), url(r'^logout/$', views.logout_view, name="logout"), ]
Now inside the logout view just log the user out.It’s typically best practice to get a post request,not a get request.So you don’t visit a url like accounts/logout.You make some kind of post request by clicking a button which will make a post request to /account/logout/ and log the user out of the application.So because it’s best to use a post request and we need to check that the post request was made.So to do that change the accounts/views.py file by adding this function in that file.
from django.shortcuts import render,redirect from django.contrib.auth.forms import UserCreationForm,AuthenticationForm from django.contrib.auth import login,logout def signup_view(request): ''' If the request is post then we will take the data that we've got from the form and we want to somehow validate it and the way we do that is by following: ''' if request.method == 'POST': ''' What this instance is doing is essentially kind of valid in that data for us.If this data is okay(like user is already exists or not,password in long enough or not etc..).That's going to return us a instance of form that is either valid or invalid.If it is valid save that form to database. ''' form = UserCreationForm(request.POST) if form.is_valid(): #log the user in user = form.save() #after getting user we will try to login that user login(request, user) #redirect the user to articles list page return redirect('articles:list') #if the request is get then create a blank instance of user creation form. else: #create a new instance of the form form = UserCreationForm() #send that form to template return render(request, 'accounts/signup.html',{'form':form}) def login_view(request): ''' same as the signup view,in the login we can also have get request or post request if the request is post ''' if request.method == 'POST': form = AuthenticationForm(data=request.POST) if form.is_valid(): #log the user in #first of all we need to get what the user is,who's trying to log in user = form.get_user() #after getting user we will try to login that user login(request, user) #redirect the user to articles list page return redirect('articles:list') else: #this will be get request.When user clicks on login link or he will go to /accounts/login/ #we will send them a login form to the template and render that form in the browser #create a new instance of the form form = AuthenticationForm() return render(request, 'accounts/login.html',{'form':form}) def logout_view(request): if request.method == 'POST': #we don't need to pass the current user becuase he is already logged in as a user. #it will logout the current user logout(request) #once the user is logged out we'll do some kind of redirect. return redirect('articles:list')
Now all we need to do is show up some kind of button in the articles list page which will logout the user out,but as a user we want to show a logout button on every page.So how to do that?Remember we have one base_layout template,which we are extending in every template,we can write one logout button,it will include logout button in every page ,because we are extending the base_layout template in every template.
So change the base_layout.html template as following to add a button for template as following:
{% load static from staticfiles %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Djangonauts</title> <link rel="stylesheet" href="{% static 'style.css' %}"> </head> <body> <header class="wrapper"> <h1> <a href="{% url 'articles:list' %}"><img src="{% static 'logo.png' %}" alt="djangonautic" /></a> </h1> <nav> <ul> <li> <form class="logout-link" action="{% url 'accounts:logout' %}" method="post"> {% csrf_token %} <button type="submit">Logout</button> </form> </li> </ul> </nav> </header> <div class="wrapper"> {% block content %} {% endblock %} </div> </body> </html>
So now we have a logout button on every page.It will log the user out.You can check it on your own
So this is all about how to log the user out.