简体   繁体   中英

Django template - “Template” object has no attribute “split”

I am using jinja2 to render a template in 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'

Which suggests that the template is not found. This is not the case here.

render_to_string takes a template name, not an object. You don't need to call get_template .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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