繁体   English   中英

Django模板-“模板”对象没有属性“拆分”

[英]Django template - “Template” object has no attribute “split”

我正在使用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'

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

render_to_string采用模板名称,而不是对象。 您不需要调用get_template

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

暂无
暂无

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

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