Reputation: 6824
I need custom template tag inside model.TextField
value. Value from the object text field have something like "lorem ipsum dolor {% mytag %}"
but "mytag" is not rendered as template tag. It is registered in the library as tag and loaded on the page and I have {{ object.textfield|safe }}
filter. Is it possible at all?
Upvotes: 0
Views: 1581
Reputation: 2569
As Django's template engine can easily be used anywhere in your code you should be able to do something like this:
from django.template import Context, Template
rendered = Template("{% load your_tag_library %}",
object.textfield).render(Context())
Rather than rendering the template from a file it renders it from a string like:
"{% load your_tag_library %}lorem ipsum dolor {% mytag %}"
The code can for instance be used in your view or as a method on your model. Note that the Context
is empty, you might as well pass a dict with template variables to it.
Furthermore, to handle it directly in the template you could write a custom templatetag which does something similiar, basically a templatetag that parses strings for templatetags.
Upvotes: 1