Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Django template engine to render in memory templates?

I am storing my templates in the database and I don't have any path to provide for the template.render method.

Is there any exposed method which accepts the template as a string?

Is there any workaround?

like image 660
Jader Dias Avatar asked Feb 14 '10 00:02

Jader Dias


People also ask

Which template engine does Django use?

A Django project can be configured with one or several template engines (or even zero if you don't use templates). Django ships built-in backends for its own template system, creatively called the Django template language (DTL), and for the popular alternative Jinja2.

How does Django template engine work?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

What is DTL in Django?

Django Template Language (DTL) is the primary way to generate output from a Django application. You can include DTL tags inside any HTML webpage. The basic DTL tag you can include in an HTML webpage is: 1. {% Tag %}


2 Answers

Based on the the docs for using the template system:

from django.template import Template, Context  t = Template("My name is {{ my_name }}.") c = Context({"my_name": "Adrian"}) t.render(c) 
like image 99
robertzp Avatar answered Sep 28 '22 07:09

robertzp


Instantiate Template with the string to use as a template.

like image 41
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 06:09

Ignacio Vazquez-Abrams