在Windows上正确设置DJANGO_SETTINGS_MODULE [英] setting DJANGO_SETTINGS_MODULE on windows Correctly

查看:554
本文介绍了在Windows上正确设置DJANGO_SETTINGS_MODULE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此查询,我有一些帖子,但大多数都是针对Linux的.它们都不是针对Windows的.

There are my post on this query but most of them are for linux. None of them exlicitly for windows

在我的应用程序中,我正在设置数据库(sqlite3,Django中的默认设置).编辑我的应用程序(mysite)的setting.py文件后

in my application i'm setting up the database (sqlite3 , default in Django). after editing setting.py file of my application (mysite)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'D:/Django_Code/sqlite.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

在发现我必须设置DJANGO_SETTINGS_MODULE环境之后,Django才能知道有关数据库设置的信息.所以我将其设置为

after finding that i have to set DJANGO_SETTINGS_MODULE environment so that Django can know about the database setting. So i set it up like

DJANGO_SETTINGS_MODULE = "D:\Django_Code\mysite\mysite\settings.py"

在发布时交叉检查数据库设置

To cross check the database setup when i issue

>>> from django.db import connection
>>> cursor = connection.cursor()

它说DJANGO_SETTINGS_MODULE环境变量未定义.

it says DJANGO_SETTINGS_MODULE environment variable is Undefined.

需要帮助来正确设置DJANGO_SETTINGS_MODULE.

Need help to set DJANGO_SETTINGS_MODULE correctly.

推荐答案

在Windows中设置DJANGO_SETTINGS_MODULE的最简单方法是使用 set 命令.您还应该能够通过系统属性进行设置,但是您需要关闭并重新打开命令提示符,以使更改生效.

The easiest way to set DJANGO_SETTINGS_MODULE in Windows is using the set command from the command prompt. You should also be able to set it via system properties, but you'd need to close and reopen the command prompt for the changes to take effect.

您也可以使用set命令查询DJANGO_SETTINGS_MODULE的当前值:

You can query the current value of DJANGO_SETTINGS_MODULE using the set command as well:

C:\temp\testproject> set DJANGO_SETTINGS_MODULE

此外,您需要将其设置为python模块名称无法导入设置'C:\ temp \ testproject \ settings.py"(位于sys.path上)的错误. ?):不支持按文件名导入.)

In addition, you need to set it to the python module name, not the filename (setting it to the filename will give you an error similar to "Could not import settings 'C:\temp\testproject\settings.py' (Is it on sys.path?): Import by filename is not supported.")

例如

C:\temp\testproject> set DJANGO_SETTINGS_MODULE=testproject.settings

然后您可以运行python并可以导入该模块.

Then you can run python and the module can be imported.

>>> import sys
>>> sys.path += ['C:\\temp']
>>> from django.db import connection
>>> connection.cursor()
<django.db.backends.util.CursorDebugWrapper object at 0x02C7F0B0>

请注意,我们还已将包含django项目(在本例中为testproject)的目录显式添加到sys.path,该目录实际上是Python查找模块的目录列表.这是必需的,因为Python会将设置文件作为python模块而不是文件(如前所述)导入.

Note that we've also explicitly added the directory containing the django project (testproject in this case) to sys.path, which is effectively a list of directories where Python looks for modules. This is necessary because Python imports the settings file as a python module, not a file (as mentioned earlier).

如果您想让交互式外壳与Django对象一起玩,则可以使用

If you want an interactive shell to play with Django objects, you can use the shell management command. In your Django project directory, run the following command:

manage.py shell

由于您在Windows上,因此您可能必须这样做

Since you're on windows, you may have to do

python manage.py shell

相反,由于我个人遇到了python脚本无法以其他方式接收命令行参数的问题.

instead, since I've personally had issues with python scripts not receiving command line arguments otherwise.

C:\temp\testproject> python manage.py shell

In [1]: from django.db import connection

In [2]: connection.cursor()
Out[2]: <django.db.backends.util.CursorDebugWrapper at 0x33a0bd0>

(请注意,我已经安装了 IPython ,并且Django很聪明并且正在使用它;如果您没有IPython,安装后,您的外壳看起来会略有不同.)

(Note that I have IPython installed and Django is being clever and using that; if you don't have IPython installed, your shell will look slightly different.)

如果要使用Django运行脚本,最简单的方法是

If you want to run a script with Django, the easiest way is to write a custom management command, which you can then run with an argument to manage.py.

这篇关于在Windows上正确设置DJANGO_SETTINGS_MODULE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆