Open In App

What is the Minimum Length of Password in Django?

Last Updated : 09 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Django, the default minimum period for passwords is 8 characters. This method that while you create a personal account or alternate a password, Django enforces a minimal password period of eight characters by default. However, you could customize this minimum length by enhancing your mission’s settings.

In Django, you could set the minimum duration of a password by configuring the AUTH_PASSWORD_VALIDATORS putting to your venture’s settings file (generally settings.Py). Django lets you define a listing of password validators that put into effect various password regulations, consisting of minimal duration necessities.

Minimum Length of Password in Django

In the Django Project, we have introduced the MinimumLengthValidator to the AUTH_PASSWORD_VALIDATORS list and distinct the min_length option as eight characters. This approach is that when customers create or change their passwords, Django will make certain that the password is at least eight characters long.

Here’s an example of how to set a minimum password length of 8 characters the steps are:

  • To update the minimum password length in your Django project (using the “ProductStore” project as a reference), follow these steps:
  • Open the project_name/settings.py file in your project.
  • Locate the AUTH_PASSWORD_VALIDATORS setting. It’s a list of dictionaries that define the password validation requirements.
  • Within the AUTH_PASSWORD_VALIDATORS list, you’ll find a dictionary with the ‘NAME’ key set to ‘django.contrib.auth.password_validation.MinimumLengthValidator’. This validator enforces the default minimum password length of 8 characters.
  • To change the minimum password length, you can modify the ‘OPTIONS’ dictionary for this validator. Add a ‘min_length’ key with the desired minimum length. For example, to set a minimum password length of 10 characters, you can update the validator like this:

Code Implementation

Create a project by the following command:

django-admin startproject ProductStore
cd ProductStore

Update AUTH_PASSWORD_VALIDATORS

Go to the settings.py and find the following code, Here, we set the minimum length of the password should be 11 characters.

Python3




AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        'OPTIONS': {
            'min_length': 11# Change this to your desired minimum length
        }
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


Create superuser

Creating a superuser in Django is essential for administrative tasks and accessing the Django admin interface, allowing full control over the application’s database and content management.

python manage.py createsuperuser

Screenshot-from-2023-10-03-10-51-02

Migrate your Data to the Database

Migrating data to the database in Django is essential to ensure that your application’s data schema aligns with the defined models, enabling proper data storage, retrieval, and consistency.

python manage.py makemigrations
python manage.py migrate

Deploy Django Project

Deploy the Project using the following command

python manage.py runserver

Now go to following Django admin pannel, and login with username and password.

http://127.0.0.1:8000/admin/

Go to add user and create new user, if you enter password less than 11 character you will get an error as shown below:

Screenshot-from-2023-09-22-01-09-41



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads