【问题标题】:Template issue with djangoDjango的模板问题
【发布时间】:2012-05-14 20:01:58
【问题描述】:

我正在尝试使用 django 创建一个模板,但是当我在 Python Shell 中执行这些语句时

>>> from django.template import Template
>>>t = Template("My name is {{ my_name }}.")

它给了我这个错误

  Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    t = Template("Mi nombre es {{ mi_nombre }}.")
  File "django\template\base.py", line 123, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "django\utils\functional.py", line 184, in inner
    self._setup()
  File "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.

有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: python django templates django-templates


    【解决方案1】:

    不要直接运行 python shell,而是使用 Django 的包装器。

    cd 到你的项目目录,然后运行

    python manage.py shell
    

    这将为您提供一个交互式 shell,其中已设置 Django 并已导入您的设置。

    还有其他方法可以实现这一点,但这应该能让你继续前进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-16
      • 2012-01-26
      • 1970-01-01
      • 2012-11-07
      • 2018-05-04
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多