0

简短说明 - 我正在尝试调用from django.middleware import csrfpython shell。它抛出/显示一条消息。

查看外壳代码:

C:\>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> from django import middleware
>>> from django.middleware import csrf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\middleware\csrf.py", line 16, in <m
odule>
    from django.utils.cache import patch_vary_headers
  File "C:\Python27\lib\site-packages\django\utils\cache.py", line 26, in <modul
e>
    from django.core.cache import get_cache
  File "C:\Python27\lib\site-packages\django\core\cache\__init__.py", line 70, i
n <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 53, in __ge
tattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in _set
up
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but setti
ngs are not configured. You must either define the environment variable DJANGO_S
ETTINGS_MODULE or call settings.configure() before accessing settings.
>>>

任何人都可以经历这个以及如何解决这个问题?

4

1 回答 1

0

您无法从 python shell 访问和运行 django 项目。Django 不知道你想从事什么项目。

您必须执行以下操作之一:

1. python manage.py shell
2. Set DJANGO_SETTINGS_MODULE environment variable in your OS to mysite.settings
3. Use setup_environ in the python interpreter:
   from django.core.management import setup_environ
   from mysite import settings
   setup_environ(settings)
于 2013-08-06T10:59:32.513 回答