Django User Authentication in 5 Minutes

A neat UI alongside a lucid back-end to build a perfect authentication system

Samhita Alla
Oct 29 · 3 min read

Let’s not make a mountain out of a molehill in understanding and coding authentication in django. Authentication, as we all know, is a basic necessity required for the users’ to pass through, before they encounter the actual treasure or the application in general. It’s a protective gear to be worn by an application to ensure secure access to it. Here, I’m going to walk you through 5 simple steps that shall help you in integrating authentication with your django application.

Image for post
Image for post
Background photo from Unsplash

To start a django project -

django-admin startproject auth_django .

Step 1: Handling settings in

Assuming we’ve a project set up, let’s quickly define a few authentication URLs in the file in the project (auth_django) folder (an app isn’t being considered due to the simplicity of the application).

# Login & Logout URLs
LOGIN_URL = '/login/'

Also, initialize the path to templates/ folder in ‘DIRS’.

'DIRS': [os.path.join(BASE_DIR, 'templates/')],

Note: Create a folder named templates/ in parent auth_django/ directory.

Step 2: Defining Views in

Django Views is where the logic resides. Create file in auth_django/auth_django/ path. We define two views — one to help the user register, and the other, to display the home page. Login and Logout views are taken from the predefined auth_views (to be seen in Step 3).

The home view renders a success page. @login_required is to make sure that home isn’t accessible to unauthenticated users’. The register view initially displays a UserCreationForm that prompts the user for username, password and confirm password. When a user submits the form, the details are captured and validated. If validation is successful, details are saved in the default database (SQLite) and the user is taken to the home page via authentication.

Step 3: Initializing URLs in

Authentication URLs are all associated with views in home and register URLs are mapped with the views in login and logout are mapped with the inbuilt auth_views. as_view() is a method in the class LoginView/LogoutView which returns a callable view.

Step 4: Creating Templates

Users’ understand the application through templates. login view by default uses registration/login.html in templates/ folder. Thus, we define all the templates in registration/ folder.

  1. base.html — Encloses external libraries

2. login.html — Login Form

3. register.html — Registration Form

4. success.html — Home page

Project Directory Structure -

Image for post
Image for post
Project Directory Structure

Step 5: Code Harvest!

Handy commands:

To migrate the database -

python3 migrate

To run server -

python3 runserver
Image for post
Image for post
User Registration Window
Image for post
Image for post
User Login Window
Image for post
Image for post
Home Page

I hope this article has helped you. Thanks for reading!

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store