Python - Django Simple Login and Logout

Getting Started

First we're gonna start a new project named dproject. If you have no idea how to create a new project you may refer to my tutorial Python Getting Started with Django.

Creating our Admin

We're gonna create our admin so that we can have our sample user. 1. After creating our project, go to command prompt and go to your project directory by typing cd dproject. Note: dproject is my project name. 2. We then make our migrations by typing the ff two lines in our command prompt: python manage.py makemigrations python manage.py migrate 3. We now create our admin by typing: python manage.py createsuperuser It will then give you the form for the admin account. Fill up the form and don't forget your account.

Setting up our URLs

In your main app, in my case in dproject, open urls.py and change the content with the following codes:
  1. from django.conf.urls import url
  2. from django.contrib import admin
  3. from django.contrib.auth import views as auth_views
  4. from django.views.generic.base import TemplateView
  5.  
  6. urlpatterns = [
  7.     url(r'^admin/', admin.site.urls),
  8.     url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
  9.     url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
  10.     url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
  11. ]

Editing our Settings

Next, we edit our settings. Open settings.py located in your main app, in my case in dproject and edit the ff: 1. Find TEMPLATES and change it with the ff codes:
  1. TEMPLATES = [
  2.     {
  3.         'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4.         'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
  5.         'APP_DIRS': True,
  6.         'OPTIONS': {
  7.             'context_processors': [
  8.                 'django.template.context_processors.debug',
  9.                 'django.template.context_processors.request',
  10.                 'django.contrib.auth.context_processors.auth',
  11.                 'django.contrib.messages.context_processors.messages',
  12.             ],
  13.         },
  14.     },
  15. ]
2. Paste this code at the bottom of settings.py
  1. LOGIN_REDIRECT_URL = 'home'

Setting up our Static Files

This is optional since our static file contains our bootstrap. The file is included in the source file of this tutorial.

Setting up our Templates

We now then set up our pages. base.html
  1. <!DOCTYPE html>
  2.   <meta charset="utf-8">
  3.   <title>{% block title %}Django Simple Login{% endblock %}</title>
  4.   {% load staticfiles %}
  5.   <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type="text/css">  
  6. </head>
  7.   <div class="container">
  8.   <header>
  9.     <h1>Django Simple Login</h1>
  10.     by <a href="https://www.sourcecodester.com/user/224918/track">nurhodelta_17</a>
  11.   </header>
  12.   <hr>
  13.   <main>
  14.     {% block content %}
  15.     {% endblock %}
  16.   </main>
  17.   <hr>
  18.   </div>
  19. </body>
  20. </html>
home.html
  1. {% extends 'base.html' %}
  2.  
  3. {% block title %}Login{% endblock %}
  4.  
  5. {% block content %}
  6.         a<h2>Welcome!</h2>
  7.         {% if user.is_authenticated %}
  8.       Hi {{ user.username }}!
  9.       <a href="{% url 'logout' %}" class="btn btn-danger">Logout</a>
  10.     {% else %}
  11.       <a href="{% url 'login' %}" class="btn btn-primary">Login</a>
  12.     {% endif %}
  13. {% endblock %}
login.html
  1. {% extends 'base.html' %}
  2.  
  3. {% block title %}Login{% endblock %}
  4.  
  5. {% block content %}
  6.   <h2>Login</h2>
  7.   <form method="post">
  8.     {% csrf_token %}
  9.     {{ form.as_p }}
  10.     <button type="submit" class="btn btn-primary">Login</button>
  11.   </form>
  12. {% endblock %}
logout.html
  1. {% extends 'base.html' %}
  2.  
  3. {% block title %}See you!{% endblock %}
  4.  
  5. {% block content %}
  6.   <h2>Logged out</h2>
  7.   <p>You have been successfully logged out.</p>
  8.   <p><a href="{% url 'login' %}" class="btn btn-primary">Log in</a> again.</p>
  9. {% endblock %}

Running our Server

We now run our server and check if everything is working as expected. You're index page should look like this: loginindex Login using the admin account the you have created earlier to check if it is working. That ends this tutorial. Happy Coding :)

Add new comment