关于Pycharm的Django:与DJANGO_SETTINGS_MODULE配置不当

时间:2015-08-03 21:02:31

标签: django python-3.x django-views pycharm django-settings

我正在尝试使用Pycharm Community Edition来改进我的Django应用程序中的代码,但是我无法运行我想要的所有Django代码。我不断得到这个追溯......

Traceback (most recent call last):
 File "C:/Users/Jaysp_000/firstSite/PROJECTone/blog_static/views.py", line 1, in <module>
   from django.views.decorators.csrf import csrf_exempt
 File "C:\Python34\lib\site-packages\django\views\decorators\csrf.py", line 3, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
 File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
 File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import caches
 File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 42, 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.

此错误似乎涉及我导入到views.py文件中的django.views.decortors.csrf.csrf_exempt。我尝试过其他文件,他们没有给我任何问题。关于这个导入有一些特别的东西,但我不知道是什么。

from django.views.decortors.csrf import csrf_exempt

@csrf_exempt
def handle_hook(request):
    from django.http import HttpResponse
    from django.core.management import call_command
    result = call_command('update_blog', verbosity = 0)
    return HttpResponse(result)

当我尝试在python shell(我使用3.4)和我import django.http.request as request上运行代码时,会出现同样的问题。我输入handle_hook(request),我得到同样的错误。

我被告知我必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure(),但我不知道如何做到这一点。我环顾四周,我不确定这些方法是否具体说明了我的问题。有线索吗?

1 个答案:

答案 0 :(得分:4)

转到“运行”菜单,选择“编辑配置...”,然后为您的测试选择运行配置。

选择环境变量按钮。您将看到一个现有变量,即PYTHONUNBUFFERED 在该添加下(例如)DJANGO_SETTINGS_MODULE = mysitename.settings

enter image description here