I’ve been working on a means for users to store their own templates and I know that I’m going to have to create Templates on the fly. For example:
>>> from django.template import Template
>>> Template("Testing {{test}}")
<django.template.base.Template object at 0x10c58f990>
However, when I asked about loading template strings, I was advised to use get_template_from_string:
>>> from django.template import loader
>>> loader.get_template_from_string("Testing {{test}}")
<django.template.base.Template object at 0x10c8b5450>
What is the difference between the two methods? Is one way more pythonic, or preferred to the other?
The Django documentation for the templating code describes using the first method you’ve described: using
Template().It does not mention
get_template_from_string, nor does anywhere else in the documentation.I would therefore lean heavily on usage of the first method, because the other is presumably more likely to change in future, as it is an undocumented function.