Create A User Logout In Django


Not Complete
Views: 162
Published Date: Oct. 23, 2018, 1:22 p.m.
Section: User Authent...

Join Our Mailing List

Get the latest tutorials right to your inbox and you can unsubscribe anytime.


How To Create A User Log Out In Django 2.+

In this Django tutorial, we will show you how to log out a user in our CMS. When it comes to logging a user out with Django Authentication package you find this is probably the easiest functionality we will add to our Django application. 

The logout functionality comes with Django's Authentication package which is basically a view that is contained in the package. We simply add a URL and a link and we can log out a user. Normally I do not provide a special template for the logout process since web standard seems to be to redirect a user to either the home page or login page. In this tutorial when I user logs out, we will redirect a user to the login page. 

Step 1: Let's start off this tutorial by adding the view via the urls.py file. Open your accounts urls.py file and add the following code to the urlpatterns.

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(next_page='accounts:login'), name="logout"),
]
  • 'logout/' - We first set the URL to http://127.0.0.1:8000/accounts/logout/ 
  • auth_views.LogoutView.as_view() - We then bring in the default logout view from the Authentication package. 
  • next_page='accounts:login' - We now provide a keyword argument which tells Django what our next page will be after the log out in this case our next page will be the login page.
  • name="logout" - We name this URL so we can easily call it in the next step.

Step 2: Now we need to add a link that will allow us to log out a user. We add the link to our base.html file. This link will go in the top right of the nav bar. Change the last part of the navbar to match our code below.

    <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#">Disabled</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="https://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
            <div class="dropdown-menu" aria-labelledby="dropdown01">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
        </ul>
        <ul class="navbar-nav"> <!-- add here -->
            <li class="nav-item">
                <a class="nav-link" href="{% url 'accounts:logout' %}">Logout</a> 
            </li>
        </ul> <!-- to here -->
      </div>
    </nav>

In the next tutorial, we will change this HTML code around to be smarter and change the links based on if the user is logged-in verses logged-out.

Conclusion

We have now learned how to log in a user and log out a user in Django. In the next tutorial we will make our HTML template a little smarter then we will move on to make our application a lot smarter by creating a signup and much more.



Ask A Question


Similar Tutorials


Create A User Logout In Django

User Log In

Last 5 Tutorials


The Array Object

Printing NumPy Arrays

How To Create An NumPy Array

Key NumPy Attributes

NumPy Array Vs. List

Most Popular


Introduction To Command Line and Python Commands

Install Python 3.7 On Mac OS X

Introduction To Python 3.7 Tutorial Series

How To Use The Django For Loop Counter

Upload A CSV File To Django