简体   繁体   English

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

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

I am using jinja2 to render a template in django: 我正在使用jinja2在django中渲染模板:

Template settings: 模板设置:

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/"]},
]

Calling code: 调用代码:

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

html_message = render_to_string(html_template, context)

This gives the error: 这给出了错误:

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

Googling this only shows solutions for: 谷歌搜索仅显示以下解决方案:

AttributeError: 'NoneType' object has no attribute 'split' AttributeError:'NoneType'对象没有属性'split'

Which suggests that the template is not found. 这表明找不到模板。 This is not the case here. 这里不是这种情况。

render_to_string takes a template name, not an object. render_to_string采用模板名称,而不是对象。 You don't need to call get_template . 您不需要调用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