How to Import Settings in Django

Shubham Vora Feb 02, 2024
  1. Default Settings in the Django App
  2. Custom Settings in the Django App
  3. from django.conf import settings VS. import settings
How to Import Settings in Django

In this tutorial, we will learn the difference between from django.conf import settings and import settings, which we use to import settings in the Django project.

Let’s first understand what does settings file contains in the Django app. So, the settings.py file is the normal Python file in the Django project, which contains your Django app’s configurations.

It can be empty also if you don’t have any configurations for your app, but it never happens.

Default Settings in the Django App

When we create the new Django project, it contains the settings.py file with all the initial configurations. Users can import the default settings file into other files using the code below.

from django.conf import settings

Here, django.conf is a module from which we import the settings object. If users try to import settings like the one below, that won’t work.

# This won't work
from django.conf.settings import SECRET_KEY

After importing the settings from the django.conf module, users can use it like below.

from django.conf import settings
# Do something with the properties of the settings object
if settings.SECRET_KEY:
    # some code

This way, we can import settings from the global settings file and use them inside another file.

Custom Settings in the Django App

It is not necessary to always use the default settings. Users can customize the settings also according to their requirements.

First, we will look at changing the values of default settings.

Developers can use Python’s configure() method to change the value of properties of the settings object. We can pass as many arguments as we want while calling the configure() method by taking the settings object as a reference.

It counts the settings property and values as single arguments that users can see in the code below.

from django.conf import settings
settings.configure(SECRET_KEY= < New_Security_key >)

If users try to change default settings like the one below, it will raise an error.

from django.conf import settings

# This code will raise an error
if settings.SECRET_KEY:
    settings.SECRET_KEY = "New Key"

Also, users can create a new file for the settings and add custom settings inside according to the Django app’s requirements. While creating the new file for manual settings, ensure that all letters of the file name are in uppercase and another file with that name doesn’t exist already.

Suppose users have created the custom_settings.py file. Then, users can import settings from that represented in the code below.

Also, users need to use the configure() method to override the default settings.

from django.conf import settings
from custom_settings import custom_settings

settings.configure(default_settings=custom_settings)

This way, we can change the default settings of the Django app by importing settings from other files and overriding them using the configure() method of Python.

from django.conf import settings VS. import settings

Now, let’s clarify the difference between the from django.conf import settings and import settings.

So, when a user tries to import settings using the from django.conf import settings, the app will import the settings from the default settings, which means from the global settings file.

We may have multiple other files for the custom settings in our app. So, if we import settings using import settings, the app will import the first settings file that it finds in the system.

So, it is a best practice to use the from django.conf import settings to import settings from the global settings file without trouble.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub