My django environment variable is simply set
A friend emailed me about the setting of django environment variables. He should want to set up a separate module under the django project to synchronize the relevant data to the database.
In this way, by running the file, you can use the simple model operation ready-made by django to achieve the purpose of synchronizing data. Here is a screenshot of his question:
From the error prompt, it should be that the environment variable is not set properly, which makes it impossible to make use of django-related modules and data.
My reply to him was:
Import os, sys
Dirname = os.path.dirname (_ _ file__) # current working directory
Sys.path.append (dirname)
Import settings # Assumed to be in the same directory.
Setup_environ (settings)
From django.db import connection
In this way, the current file can call the relevant modules and properties of the system. I hope it will be helpful to you.
For more information about from and import import methods, see:
Http://www.iplaypython.com/jinjie/from-import.html
Http://docs.python.org/2.7/tutorial/modules.html#importing-from-a-package
Http://hi.baidu.com/weiweisuo1986/item/ab85001259d059dabf9042ce