[Django]-How to compile a template saved in database in django

3👍

There are two methods:

  1. Use from_string() : https://docs.djangoproject.com/en/2.1/ref/templates/api/#django.template.Engine.from_string

  2. Use Template object directly.

Example.

    from django.template import Template

    template = Template("My name is {{ my_name }}.")

2👍

You can use django’s template.Template Class

from django import template

html_template_str = "<html> Hello {{ Name }} </html>"

t = template.Template(html_template_str)
c = template.Context({'Name':"Name"})
html = t.render(c)

html will contain rendered template html

Leave a comment