How to Create a Logout Page in Django


In this article, we show how to create a logout button in Django.

This gives the website logout functionality. A signed in user can click the button ( or link) and log out.

So in this article, we go through all the code that needs to be executed within a Django project to give logout functionality.

This article assumes that you have already created a signup page and a login page. However, if you need instruction on how to do so, you can see the previous links. File

So the first thing we will do is put the URL for the logout page in the file.

We will call the url, logout

This is being done in the accounts app, so the full path to the logout page, will be, /accounts/logout

This is the file in the accounts app.

The code is shown below.

We import the view, pagelogout, from (which we get to next).

This means that when the URL, /accounts/login, is entered into the browser, the pagelogin function is executed.

So this establishes the file. File

Now we will create the file that gives our website logout functionality.

In the file, we logout whatever user is currently logged in.

The code is shown below.

So, again, our function is pagelogout to give logout functionality.

Because we want the request method to be POST, rather than a GET method, we put in the line, request.method == "POST":

We do not want the logout function to be retrieved through a GET method, because some browsers automatically prefetch data through GET methods and return them (even when not explicitly asked). Therefore, a POST method is more safe to use, so that users will only be logged out if they explicitly click the logout button.

Inside of this if statmeent, we have the line, logout(request)

The line, logout(request), is all that is needed to log out the current user (request.user).

After the user logs out, we then want to redirect him/her to the home page. At this page, we then display sign up or log in at the top of the page (if the user wants to log in again).

The 'home' name is specified in the directory in the root project directory (which contains the file).

Template Files

Now we will go on to the template files.

So we will just use one template file as an example. But this code really should go into every single page of your website (logout functionality should exist on every page, maybe not sign up and login, however).

We will do this with the home page in this example.

We'll show the code first and then explain it below.

So on the home page, we have all the posts by all the users of the website.

The first thing we want to do is check whether the user is authenticated or not (logged in or not).

If the user is authenticated, we want to list the user's name and have a Logout button if the user wants to log out.

Now in order to make a POST request when the logout button is clicked, we use Javascript in order to make this occur. We use the onClick() event handler in Javascript, which executes the submit() function when the button is clicked. We put this event handler with an anchor tag with an href of # (it doesn't go this address). So once the user clicks this anchor tag, Javascript finds the element that has an id of logoutform. It goes to this form and submits it and goes to the URL, 'accounts:logout'. The whole purpose of this is that we want to go to the URL, 'accounts:logout' (/accounts/logouts/), which executes the function-based view, pagelogout, and sends this a POST request. This will log the user out.

If a user is not authenticated, then we have 2 options, Sign up or Log in.

We then have our regular page in which we show all posts that users have entered in our site.

And this is all that is required to create a logout page in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...