Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is purpose of django.setup()?

Tags:

python

django

I am updating website of my previous employee & have some issue with following code.

What is purpose of django.setup() line in following code. Is it necessary to put below code in init.py file.

from __future__ import absolute_import, unicode_literals
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sparrow.settings")
django.setup()
like image 844
Moon Avatar asked Jun 25 '19 06:06

Moon


1 Answers

It is used if you run your Django app as standalone. It will load your settings and populate Django's application registry. You can read the detail on the Django documentation.

As mentioned in the docs, django.setup() may only be called once. So, if you are facing an issue with the code, you could try modify the code into this:

from __future__ import absolute_import, unicode_literals
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app_name.settings")
if __name__ == '__main__':
   django.setup()
like image 174
Imaduddin A Majid Avatar answered Oct 29 '22 09:10

Imaduddin A Majid