Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

My django environment variable is simply set

Shulou Source: shulou.com Published: 2022-06-03 05:40:24 11月24日 Update

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

Tags: Data modules variables environment files synchronization running in which case references properties screenshots databases methods friends directories purposes systems materials emails questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Huawei MySQL macOS