eugene
eugene

Reputation: 41665

How to call django template tag from python code?

I'm writing a very simple custom tag and I want to call django's include tag from my custom tag code.

e.g. How do I call django.templates.loader_tags do_include (or include) tag from a python code?

Upvotes: 3

Views: 1422

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53981

I think you would be easier simply rendering the template yourself with the context passed to your template tag instead of using the include tag. For example:

from django.template.loader import render_to_string

@register.simple_tag(takes_context=True)
def my_tag(context):
    html = render_to_string("my_tag_template.html", context)
    ...

That said, if you want to see how the include tags work you can see the code here:

https://github.com/django/django/blob/master/django/template/loader_tags.py#L124

Upvotes: 2

Related Questions