27

I am taking an online Django class now, and I don't understand some of the configuration code. My instructor says that the following code is required in a Python code that populates my database.

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ProTwo.settings')

import django
django.setup()

I don't understand what this code is doing at all, except that the second argument in setdefault() is referring to my app's settings.py file.

What is DJANGO_SETTINGS_MODULE? What does it do? Why is it passed in as an arg in setdefault()?

And what does django.setup() do? What does it change?

4
  • 2
    Google is your friend. First result for searching "DJANGO_SETTINGS_MODULE" is this: docs.djangoproject.com/en/2.0/topics/settings Now try the same thing for yourself with "python setdefault" and maybe "django.setup()".
    – Tom Dalton
    May 14, 2018 at 13:17
  • 2
    DJANGO_SETTINGS_MODULE, I didn't find that one when I looked up, and setdefault(), I couldn't understand what I googled. There were too much jargons.
    – Eric Kim
    May 14, 2018 at 13:25
  • 2
    Try creating a dictionary in the Python shell, and call setdefault to see what it does. For example: d={}; d.setdefault('DJANGO_SETTINGS_MODULE', 'ProTwo.settings'); print(d); d.setdefault('DJANGO_SETTINGS_MODULE', 'other_project.settings'); print(d);
    – Alasdair
    May 14, 2018 at 13:35
  • Having a grasp of Python basics before learning Django is important, but in this case I wouldn't worry too much about those four lines. It's boilerplate required to use your Django settings and models in the script to populate your database.
    – Alasdair
    May 14, 2018 at 14:12

6 Answers 6

4

First look at os.environ.setdefault(), environ is an instance of class _Environ, which inherits from IterableUserDict, and IterableUserDict inherits from UserDict, including setdefault().

This method is also a method of UserDict, take a look at the functions implemented by setdefault.

def setdefault(self, key, failobj=None):
if key not in self:
    self[key] = failobj
return self[key]

The key is DJANGO_SETTINGS_MODULE, where self is a dictionary to determine if the key is inside, and do a key-value binding if it is not inside. Then return.

And here is the original link: Django搭建及源码分析(二)

1

In some cases, you might want to bypass the DJANGO_SETTINGS_MODULE environment variable. For example, if you’re using the template system by itself, you likely don’t want to have to set up an environment variable pointing to a settings module.

In these cases, you can configure Django’s settings manually.

https://docs.djangoproject.com/en/2.1/topics/settings/

1

The above command creates a mapping and sets the default for the script with key as'DJANGO_SETTINGS_MODULE' and value as 'ProTwo.settings'

additionally, When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.

1
  • what would be the difference between os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.production") and set DJANGO_SETTINGS_MODULE=mysite.settings.production ??? Also, I got the first one from wsgi.py but as far as I understand, the second one should go in settings.base , on top of the file ?
    – illevens
    Jul 17, 2020 at 19:36
1

Required when running standalone scripts. Or scripts that required the use of Django application


For example: A standalone script in Django requires this import

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_project.settings')

import django
django.setup()

This import is required so that the script can use the functionality of Django which is not present in standalone python scripts. like populating the django models with fake data.

from faker import Faker
import random
# import models only after calling django.setup()
from first_app.models import Webpage, Topic, AccessRecord

Notice the use of Django while creating model objects requires the use of Django and its setting configurations.

0

Calling django.setup() is required for “standalone” Django usage¶ If you’re using components of Django “standalone” – for example, writing a Python script which loads some Django templates and renders them, or uses the ORM to fetch some data – there’s one more step you’ll need in addition to configuring settings.

After you’ve either set DJANGO_SETTINGS_MODULE or called configure(), you’ll need to call django.setup() to load your settings and populate Django’s application registry.

0

as mentioned in Django doc:

"In your live server environment, you’ll need to tell your WSGI application what settings file to use. Do that with os.environ"

You can also add your customized config in 'settings.py' and use them in your code later:

from django.conf import settings
print(settings.my_customized_config)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.