【问题标题】:Django template - "Template" object has no attribute "split"Django模板-“模板”对象没有属性“拆分”
【发布时间】:2019-08-13 17:06:21
【问题描述】:

我正在使用 jinja2 在 django 中渲染模板:

模板设置:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ]
        },
    },
    {"BACKEND": "django.template.backends.jinja2.Jinja2", "DIRS": ["utils/"]},
]

调用代码:

html_template = get_template("email/contact/contact.html") #completes successfully - template definitely found

html_message = render_to_string(html_template, context)

这给出了错误:

AttributeError: 'Template' object has no attribute 'split'

谷歌搜索仅显示以下解决方案:

AttributeError: 'NoneType' 对象没有属性 'split'

这表明找不到模板。这不是这里的情况。

【问题讨论】:

    标签: django django-templates jinja2


    【解决方案1】:

    render_to_string 采用模板名称,而不是对象。您无需致电get_template

    html_message = render_to_string("email/contact/contact.html", context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2017-04-23
      相关资源
      最近更新 更多