3

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.