【问题标题】:environment variable for django settings?django设置的环境变量?
【发布时间】:2016-10-19 05:03:27
【问题描述】:

我已经使用 Django 一年多了,从来不需要像以下错误消息中要求我那样定义环境变量 DJANGO_SETTINGS_MODULE:

E
======================================================================
ERROR: setUpClass (__main__.SmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1026, in setUpClass
    if not connections_support_transactions():
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 991, in connections_support_transactions
    for conn in connections.all())
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 227, in all
    return [self[alias] for alias in self]
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 224, in __iter__
    return iter(self.databases)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 157, in databases
    self._databases = settings.DATABASES
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我正在使用 Windows 命令行。

我试着跑了

from django.conf import settings

settings.configure()

正如here 解释的那样,但这没有帮助。

有谁知道如何让它工作?

【问题讨论】:

  • 你的 settings.py 中有这些数据库设置吗?此外,您是否使用 Windows 以外的操作系统并从 .env 文件加载设置?
  • @MukundGandlur 我的 settings.py 中有默认的 sqlite,没有改变它。我一直在使用 Windows。
  • 你是如何运行导致这个错误的代码的?
  • @brunodesthuilliers 在命令行中我在我的应用程序目录中,我运行 python tests.py
  • 你是如何运行这些测试的?

标签: python django environment-variables


【解决方案1】:

你确定在你的 ma​​nage.py 中有这样的东西

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectname.settings")

https://docs.djangoproject.com/en/1.9/ref/django-admin/

【讨论】:

【解决方案2】:

"@brunodesthuilliers 在命令行中我在我的应用程序目录中,我运行 python tests.py"

好吧,那是行不通的(至少不是那么容易)。为什么不直接使用内置管理“测试”命令?

 #> cd your/project/root 
 #> python manage.py test

cfhttps://docs.djangoproject.com/en/1.9/topics/testing/

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 2019-07-10
    • 2013-03-11
    • 2019-12-24
    • 1970-01-01
    • 2018-07-13
    • 2014-12-17
    • 2014-06-21
    • 2016-12-26
    相关资源
    最近更新 更多