繁体   English   中英

在Django / Python中创建模板

[英]Creating templates in Django / Python

我正在学习Django ,我试图创建一个这样的模板:

>>> weekend = True
>>> from django.template import Template, Context
>>> template_string = """
{% if weekend %}
    <p> This is a weekend </p>
{% endif %}
"""
>>> t = Template(template_string)

我得到以下错误。 我错过了什么吗?

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    t = Template(template_string)
  File "C:\Python27\lib\site-packages\django\template\base.py", line 106, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 276, in __getattr__
    self._setup()
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

谢谢!

这与模板无关。

通过从包含manage.pysettings.py的目录执行python manage.py shell来启动shell。

如果您已经启动了django项目,请尝试在django shell中运行相同的代码。

python manage.py shell

或者将DJANGO_SETTINGS_MODULE设置为您的设置模块的名称。 如果您正在使用linux并从settings.py文件所在的目录运行,那么您可以这样做

export DJANGO_SETTINGS_MODULE="settings"

如果你正在使用linux(或其他基于unix的操作系统)并且你想从另一个目录运行,你需要确保你的项目目录在python路径中。 那你可以做

export DJANGO_SETTINGS_MODULE="myapp.settings"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM