Django User Authentication Complete Example

In this Django article iam going to talk about Django User Authentication Complete Example, so Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. the Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do.

basically we are going to create login, logout, forgot password and reset password functionality.  for more information you can check the video

 

 

 

Check the complete video for Django User Authentication Complete Example

 

 

 

 

Also you can read more django articles

1: Django Pagination Complete Example

2: Django Sending Email to Gmail Account 

3: Build News Application in Django 

4: Django Aggregation Example

 

 

Authentication support is bundled as a Django contrib module in django.contrib.auth. By default, the required configuration is already included in the settings.py generated by django-admin startproject, these consist of two items listed in your INSTALLED_APPS setting:

 

  1. 'django.contrib.auth' contains the core of the authentication framework, and its default models.
  2. 'django.contrib.contenttypes' is the Django content type system, which allows permissions to be associated with models you create.

 

and these items in your MIDDLEWARE setting:

  1. SessionMiddleware manages sessions across requests.
  2. AuthenticationMiddleware associates users with requests using sessions.

With these settings in place, running the command manage.py migrate creates the necessary database tables for auth related models and permissions for any models defined in your installed apps.

 

 

 

Django provides the following class-based views to deal with authentication. All of them are located in django.contrib.auth.views:

  • LoginView: Handles a login form and logs in a user
  • LogoutView: Logs out a user
  • PasswordChangeView: Handles a form to change the user password
  • PasswordChangeDoneView: The success view the user is redirected to
  • PasswordResetView: Allows users to reset their password. It
    generates a one-time use link with a token and sends it to
    the user’s email account.

 

 

 

OK first of all you need to create a new project in django.

 

 

 

After that we are going to create an app. make sure that you have changed the directory to your project.

 

 

 

Now you need to migrate your project .

 

 

 

Also you need to create a super user, because we need to use that for our login system.

 

 

 

Login & LogoutView

First of all you need to create a new urls.py in your created app, and add this in your file.

 

 

 

Also you need to link this file to your main project urls.py like this.

 

 

 

Create a new directory inside the templates directory of your account application and name it registration. This is the default path where the Django authentication views expect your authentication templates to be.

The django.contrib.admin module includes some of the authentication templates that are used for the administration site. We have placed the account application at the top of the INSTALLED_APPS setting so that Django uses our templates by default instead of any authentication templates defined in other apps.

 

Create a new file inside the templates/registration directory, name it login.html, and add the following code to it:

 

Django uses the AuthenticationForm form located at django.contrib.auth.forms by default. This form tries to authenticate the user and raises a validation error if login was unsuccessful.

 

 

Also we have already added our home.html url at the top. you need to create that file out side of the registration folder, just you need to add that file in the main templates, these are the files that we want to add in templates folder. basically home.html is for redirecting successful login.

 

This is base.html

 

 

 

And this our home.html.

 

basically we are going to authenticate if the use is valid.

 

 

Edit the settings.py file of your project and add the following code to it:

 

 

  • LOGIN_REDIRECT_URL: Tells Django which URL to redirect after a successful login if no next parameter is present in the request
  • LOGOUT_URL: The URL to redirect the user to log out

 

 

 

Password Change 

And these are the files for our changing password. add these files in templates/registration folder.

 

 

password_change_form.html

 

 

 

password_change_done.html

 

 

 

Password Reset

Add a new file in the templates/registration/ directory of your account application and name it password_reset_form.html. Add the following code to it:

 

 

password_reset_form.html

 

 

Now, create another file in the same directory and name it password_reset_email.html. Add the following code to it:

The password_reset_email.html template will be used to render the email sent to users to reset their password.

 

 

Create another file in the same directory and name it password_reset_done.html. Add the following code to it:

 

 

 

Create another template in the same directory and name it password_reset_confirm.html. Add the following code to it:

 

We check whether the provided link is valid. The view PasswordResetConfirmView sets this variable and puts it in the context of the password_reset_confirm.html template. If the link is valid, we display the user password reset form.

 

Create another template and name it password_reset_complete.html. Enter the following code into it:

 

 

 

 

Also in your settings.py you need to add this for email confirmation.

 

 

 

 

So run the project and this will be the result

Django Login
Django Login

 

 

Django Login Successfull
Django Login Successfull

 

 

Django Reset Email
Django Reset Email

 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “Django User Authentication Complete Example”

Comments are closed.

Share via
Copy link
Powered by Social Snap
×