How to call Django environment in a Python script

created at 07-17-2021 views: 1

Call the Django environment in a Python script:

import os

if __name__ =='__main__':
     #Load the project environment, "App.settings" is the project configuration file
     #App must be your project name
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "App.settings")
     #Load django
     import django
     #Start django
     django.setup()      

     #Start running your script file
     #Specific code

Note: App is the name of the project

Please log in to leave a comment.