Django.core.exceptions.improperyconfigured:请求设置缓存,但未配置设置。你要么定义环境变量

2024-04-20 02:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尽我所能解决这个问题,我开始有点扯头发了。 我得到这个错误:

django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

当我这样做时,我的脚本运行良好:

python3 ./manage.py runserver

但是,每当我尝试运行测试时,我会得到上面的错误。。。 我使用一个VirtualEnv,它在全局上不继承任何内容,所有内容都已安装(使用正确的版本),并且在manage.py中设置了:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")

我正在使用PyCharm professional进行开发,我尝试在IDE和shell中运行测试。 在我使用的shell中:

python3 manage.py test     

壳牌没有发现任何测试。这个测试是基本的,我并不是真的那么在意它的内容,因为它是我正在挣扎的环境。更新:我已经解决了shell的问题。测试必须定义为:

def test_<name>():

不过,这并没有解决我与皮查姆的问题。 我还打电话给:

settings.configure()

它告诉我它已经配置好了。
请注意,我没有在Django中使用任何数据库,并且我已经在设置中注释了适当的内容。

完全错误是:

  Traceback (most recent call last):
  File "/root/kiloenv/lib/python3.4/site-packages/django/conf/__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "/usr/lib/python3.4/os.py", line 631, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/<username>/Documents/<dirname>/<appname>/tests.py", line 1, in <module>
        from django.test.utils import setup_test_environment
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/__init__.py", line 5, in <module>
        from django.test.client import Client, RequestFactory
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/client.py", line 11, in <module>
        from django.contrib.auth import authenticate, login, logout, get_user_model
      File "/root/virtualenv/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 6, in <module>
        from django.middleware.csrf import rotate_token
      File "/root/virtualenv/lib/python3.4/site-packages/django/middleware/csrf.py", line 14, in <module>
        from django.utils.cache import patch_vary_headers
      File "/root/virtualenv/lib/python3.4/site-packages/django/utils/cache.py", line 26, in <module>
        from django.core.cache import get_cache
      File "/root/virtualenv/lib/python3.4/site-packages/django/core/cache/__init__.py", line 69, in <module>
        if DEFAULT_CACHE_ALIAS not in settings.CACHES:
      File "/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 54, in __getattr__
        self._setup(name)
      File "/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 47, in _setup
        % (desc, ENVIRONMENT_VARIABLE))
    django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我在sudo下运行了PyCharm,以确保问题不是权限问题,因为我将env存储在根目录下。

编辑:我刚刚发现,不使用Django的测试运行良好,但Pycharm仍有几次失败。这些失败不是单独的测试,它们只是我在这里提到的错误(有341个测试与Django无关)。我只有一个使用Django的测试,它无法通过初始化和抛出上述错误。

希望我一直在解释


Tags: djangoinfrompytestsettingsvirtualenvinit
3条回答

在python脚本中,在设置环境之前尝试访问Django模型请按以下顺序进行尝试:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.`settings`")
from <appname>.models import Class1, Class2,...

用这个

import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

而不是

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")

如果您使用的是PyCharm Pro,则可以通过“运行Django控制台…”操作来测试应用程序。单击“测试”后,它将提示您输入要测试的应用程序。

enter image description here

或者

在运行/调试配置中创建Django测试。

enter image description here

相关问题 更多 >