django.core.exceptions.ImproperlyConfigured:请求设置缓存,

django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment varia(django.core.exceptions.ImproperlyConfigured:请求设置缓存,但未配置设置.您必须定义环境变量)

本文介绍了django.core.exceptions.ImproperlyConfigured:请求设置缓存,但未配置设置.您必须定义环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有能找到的方法来解决这个问题,但现在我的头发开始掉了一点.我收到此错误:

I've tried everything that I could find to fix this issue, and I'm starting to tear my hair out a little. I'm getting this error:

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.

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

My scripts run fine when I do:

python3 ./manage.py runserver

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

However, whenever I try to run tests, I get the above error... I use a VirtualEnv, that inherits nothing globally, everything is installed (with the correct version), and within my manage.py I have set:

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

我正在使用 PyCharm Proffesional 进行开发,并尝试在 IDE 和 shell 中运行测试.在我使用的外壳中:

I'm using PyCharm Proffesional to develop, and I've tried running the tests in both the IDE, and in the shell. Within the shell I'm using :

python3 manage.py test     

shell 没有找到任何测试.测试是基本的,我目前并不关心它的内容,因为这是我正在努力的环境.更新:我已经解决了 shell 的问题.测试必须由以下人员定义:

The shell is finding no tests. The test is basic, and I'm not really all that bothered about the content of it currently as it's the environment I'm struggling with. UPDATE: I have solved the issue with the shell. Tests must be defined by:

def test_<name>():

但这并没有解决我的 PyCharm 问题.我也打过电话:

However this hasn't solved my issue with PyCharm. I have also called:

settings.configure()

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

Which told me that it was already configured.
Please note that I am not using any database with Django, and I have commented the appropriate things out of the settings.

完整的错误是:

  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 存储在 root 下.

I have ran PyCharm under sudo to ensure that the issue wasn't permissions as I'm storing the env under root.

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

EDIT: I've just discovered that my tests which do not use Django are running fine, but Pycharm is still throwing several failures. These failures aren't the individual tests, they're just the error which I have mentioned here (there's 341 tests that aren't Django related). I only have one test which uses Django, which will not get past initializing and throwing the mentioned error.

希望我一直在解释

推荐答案

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

If you are using PyCharm Pro, you can either test your app by action 'Run Django Console...'. After you click 'test', it will prompt you for the app you want to test.

在运行/调试配置中创建一个 Django 测试.

Create a Django test in Run/Debug configuration.

这篇关于django.core.exceptions.ImproperlyConfigured:请求设置缓存,但未配置设置.您必须定义环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:django.core.exceptions.ImproperlyConfigured:请求设置缓存,

基础教程推荐