User Log In


Not Complete
Views: 166
Published Date: Oct. 22, 2018, 12:45 p.m.
Section: User Authent...

Join Our Mailing List

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


User Log In

We will start off our Django Authentication tutorial series with a simple tutorial which allows a user to sign into the CMS. Throughout the next several tutorials we will build out our authentication system. 

Django ships with a feature-packed authentication system which we will use throughout this series. To have access to the authentication package we need to ensure it is installed. If you open your settings.py file and look in the installed apps list you will find an app called "auth". This package holds the authentication system. If for some reason you do not have the package in your installed apps list please add the following. "django.contrib.auth" to your INSTALLED_APPS list.

Create A User Log In

Now that we checked that we have access to Django's authentication package we can now set up a user login. There is not much code that needs to be written for setting up a log in it literally takes some small configuration and we are good to go.

We will start off this series by creating a new application that will house all our authentication code. I tend to call this application "accounts".

Step 1: Let's first create a new application can call it "accounts" enter the following command in your command line.

(cms) Thomass-MacBook-Pro:cms Tommy$ python manage.py startapp accounts

Step 2: Now open up your settings.py file and add the accounts application to your INSTALLED_APPS.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

Step 3: Now open up your main urls.py file inside your cms directory. We will need to include our accounts application in our main urls.py file. We will first need to import the include function and then we create an url for our accounts.

from django.contrib import admin
from django.urls import path, include

from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('accounts/', include('accounts.urls')),
]

Step 4: Now we will create a urls.py file inside our accounts application. Add a file to your account's directory called urls.py. Inside the file add the following code to create a url for our login view. 

from django.urls import path
from django.contrib.auth import views as auth_views


app_name = 'accounts'


urlpatterns = [
    path('login/', auth_views.LoginView(template_name='accounts/login.html'), name='login'),
]
  • We first import django.urls import path to configure our URL.
  • We then import the authentication package so we can access the default login and view. We give this import a variable of auth_views this way we can call the default authentication views. This will also allow us to bring in another views.py file as views. Which we will need in the future.
  • We then bring in a LoginView() class which will allow us to log a user into the site. 
  • We then provide a template_name which tells Django that we want to use a specific template.

Step 5: Now we will create a template directory for our accounts directory. Inside your templates directory create a directory called accounts.

Step 6: We will now create a template for our login view. Create a file named login.html inside your account's directory inside our templates directory.

{% extends 'base.html' %}

{% block content %}
<div class="col-md-6 offset-3" style="margin-top: 10rem;">
    <h2 class="text-center">Log In</h2>
    <form class="col col-12" method="post" novalidate>
        {% csrf_token %}
        {{form.username}}
        {{form.password}}
        <button class="btn btn-success" type="submit">Log In</button>
    </form>
</div>
{% endblock %}

Step 7: Open up your settings.py file and add the following global variable. This will configure our log in redirect. For testing purposes we will send the signed in user to the home page.

LOGIN_REDIRECT_URL = "/"

Conclusion

We now have a log in view which will allow our users to login into our website. If you have any questions please let us know.



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