Posted by & filed under Programming.

在命令行中直接敲入Python命令进入交互模式,然后使用

from django.template import  Template ,Context
t  = Template(“Test is {{test}}”)

会导致:ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.原因是django的配置信息没有初始化
有两种解决方法,

一种是切换到你项目或者APP所在的目录使用manage.py shell命令启动交互窗口

还有一种是手动将django的配置初始化:

>>> from django.conf import settings
>>> settings.configure()

Leave a Reply

  • (will not be published)