2013-08-06 74 views
0

简短说明 - 我试图在python shell中调用from django.middleware import csrf。 其投掷/显示消息。导入模块CSRF问题:Django

见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. 
>>> 

任何人可以通过这个&了如何来解决这个?

回答

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)