VOOZH about

URL: https://www.geeksforgeeks.org/python/django-sign-up-and-login-with-confirmation-email-python/

⇱ Django Sign Up and login with confirmation Email | Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Django Sign Up and login with confirmation Email | Python

Last Updated : 12 Jul, 2025

Django provides a built-in authentication system that handles users, login, logout, and registration. In this article, we will implement a user registration and login system with email confirmation using Django and django-crispy-forms for elegant form rendering.

Install crispy forms using the terminal:

pip install --upgrade django-crispy-forms

Step 1: Create a New Django Project and App

Start a new Django project and move into the project directory:

django-admin startproject project
cd project

Create a new app called user:

python manage.py startapp user

Step 2: Set Up Templates Folder

Navigate to your user app folder and create a templates/user directory. Inside this folder, create the following HTML files:

  • index.html
  • login.html
  • register.html
  • Email.html

Your project structure should look like this:

👁 projectStructure
Project Structure

Step 3: Configure settings.py

Open your project/settings.py file and make the following changes:

1. Add 'user' and 'crispy_forms' to your installed apps:

INSTALLED_APPS = [
# Default Django apps...
'crispy_forms',
'user',
]

2. Add the crispy forms template pack setting at the end of the file:

CRISPY_TEMPLATE_PACK = 'bootstrap4'

3. Configure your email settings by adding your email credentials (replace placeholders):

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your_email@gmail.com'
EMAIL_HOST_PASSWORD = 'your_email_password' # Use app password if 2FA enabled

Step 4: Configure Project URLs

Edit the project-level urls.py (project/urls.py) to route authentication URLs:

Step 5: Configure App URLs

Create user/urls.py if it does not exist and add:

Step 6: Create User Registration Form

Create a forms.py file inside the user app and add the following:

Step 7: Define Views for Registration and Login

Edit user/views.py to handle user registration, login, and rendering pages:

Step 8: Create HTML Templates

index.html

This template includes navigation, login/logout links, and dynamic welcome messages.

Email.html

The provided HTML code is an email template for a registration confirmation message. It uses the Roboto font, has a centered thank-you message with user-specific content (username), and a horizontal line for separation. This template is designed to deliver a visually pleasing and informative confirmation email to users.

Login.html

Inside this block, it creates a centered login form with specific styling, including a black border, padding, and a rounded border. The form includes a CSRF token for security and uses the crispy filter to render form fields with enhanced formatting, along with a login button and a link to the registration page.

Register.html

This file creates a centered sign-up form with specific styling, including a black border, padding, and rounded corners. The form includes a CSRF token for security and uses the crispy filter for enhanced form field rendering, along with a sign-up button and a link to the login page for users with existing accounts.

Step 9: How to Generate a Gmail App Password (For Sending Emails)

To securely send emails through Gmail SMTP from your Django application, you need to generate an App Password and set up email confirmation to verify users during registration.

Generating a Gmail App Password

1. Go to your Google Account security settings:

Open this URL in your browser:

https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fmyaccount.google.com%2Fgeneral-light&followup=https%3A%2F%2Fmyaccount.google.com%2Fgeneral-light&ifkv=AdBytiNmhNybpYPAImjtZLcI61txPrTgaUw9MCf3aLoVpjsgQYJY29MO_P2wZBJjIk5zQShayFmp&osid=1&passive=1209600&flowName=WebLiteSignIn&flowEntry=ServiceLogin&dsh=S-734960398%3A1752268640094531

2. Verify that 2-Step Verification is enabled:

  • Under “Signing in to Google”, check if 2-Step Verification is ON.
  • If it is not enabled, click on it and follow the instructions to enable it for your account.

3. Create an App Password:

  • After enabling 2-Step Verification, return to the Security page.
  • Click on App passwords (this option appears only if 2FA is enabled).
  • Under Select app, choose Other (Custom name).
  • Enter a name such as Django App and click Generate.
  • Google will generate a 16-character app password (e.g., abcd efgh ijkl mnop).
  • Copy this app password carefully and remove any spaces when you paste it into your Django settings.

4. Use the App Password in your Django settings.py:

EMAIL_HOST_USER = 'your_email@gmail.com'
EMAIL_HOST_PASSWORD = 'your_16_character_app_password_without_spaces'

Step 10: Make Migrations and Run Server

Run migrations:

python manage.py makemigrations
python manage.py migrate

Run your server:

python manage.py runserver

👁 loginPageDjango
Login Page

Verification mail after successful login:

👁 confirmationMail
Confirmation Mail
Comment