【问题标题】:Can't run Django template engine - Django无法运行 Django 模板引擎 - Django
【发布时间】:2017-10-19 02:40:58
【问题描述】:
from django import template

t = template.Template("My name is {{ name }}.")
c = template.Context({'name' : 'Yasmika'})
print(t.render(t))

我正在学习 Python Django 的基础知识。当我尝试运行 Django 模板时,Python 解释器给了我这个错误。我该如何解决这个问题?

C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/YasmikaSH/Desktop/lern_django/lern_django/test2.py
Traceback (most recent call last):
  File "C:/Users/YasmikaSH/Desktop/lern_django/lern_django/test2.py", line 3, in <module>
    t = template.Template("My name is {{ name }}.")
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py", line 184, in __init__
    engine = Engine.get_default()
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\engine.py", line 76, in get_default
    django_engines = [engine for engine in engines.all()
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 89, in all
    return [self[alias] for alias in self]
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 86, in __iter__
    return iter(self.templates)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\utils.py", line 29, in templates
    self._templates = settings.TEMPLATES
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
    self._setup(name)
  File "C:\Users\YasmikaSH\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\conf\__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

【问题讨论】:

标签: python django


【解决方案1】:

我认为你正在尝试

print(t.render(c))

python manage.py shell

In [74]: from django import template

In [75]: t = template.Template("My name is {{ name }}.")

In [76]: c = template.Context({'name' : 'Yasmika'})

In [77]: print(t.render(c))
My name is Yasmika.

【讨论】:

  • 没错,运行t.render(t) 毫无意义。 +1
【解决方案2】:

Django 并不是真的要在 Django 项目(或类似项目)之外使用。您可以按照"Writing your first Django app" tutorial 启动一个 Django 项目。完成项目设置后,运行以下命令启动 shell:

$ python manage.py shell

这会加载一个 Python shell,其中 Django 完全配置为使用您的设置。

【讨论】:

    【解决方案3】:

    您的环境中似乎没有定义 DJANGO_SETTINGS_MODULE 设置。

    在这里查看与设置模块相关的文档 -> https://docs.djangoproject.com/en/1.8/topics/settings/

    你可以这样设置

    export DJANGO_SETTINGS_MODULE=<yoursite>.settings
    

    当你使用 Django 时,你必须告诉它你正在使用哪些设置。通过使用环境变量 DJANGO_SETTINGS_MODULE 来执行此操作。

    DJANGO_SETTINGS_MODULE 的值应该是 Python 路径语法,例如mysite.settings。请注意,设置模块应位于 Python 导入搜索路径中。

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2011-02-25
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多