从IDLE运行Django

我希望在
Windows中测试我的django项目形式IDLE shell.

我运行以下命令

from django.template import Template, Context
t = Template('test template')

但我得到以下错误.

Traceback (most recent call last):


File "<pyshell#1>", line 1, in <module>
    t = Template('test template')
  File "C:\Program Files\Python26\lib\site-packages\django\template\__init__.py", line 164, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 28, in __getattr__
    self._import_settings()
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 59, in _import_settings
    self._target = Settings(settings_module)
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 94, in __init__
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'C:\Program Files\Python26\Lib\site-packages\django\conf' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.

你能帮我吗?

最佳答案 Django需要加载几个文件,如settings.py.为了帮助完成这项任务,Django捆绑了自己的
shell(可能是IDLE).如果您安装了
IPython,Django将使用它.

要访问shell,请使用根目录中的manage.py文件:

python manage.py shell

如果您仍然希望完全在“./manage.py shell”范围之外使用IDLE,请查看manage.py文件以了解Django如何加载所有必需的文件.

作为奖励,请查看django-command-extensions shell_plus(它提供了一些不错的附加功能,例如自动加载所有模型).

点赞