template tag inside object TextField

前端 未结 1 761
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 08:58

I need custom template tag inside model.TextField value. Value from the object text field have something like \"lorem ipsum dolor {% mytag %}\" but \"m

相关标签:
1条回答
  • 2021-01-27 09:21

    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.

    0 讨论(0)
提交回复
热议问题