What is the difference between Django’s built-in include tag and custom inclusion tags?
I have read the documentation, and both seem to to achieve the same goal: render a template passing it a context or variable.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They serve different purposes. The
includetag simply includes the content from an existing template in its entirety and unmodified. A custom inclusion tag passes the context to a function which can contain logic to manipulate the context before passing it to a template.For example, perhaps I have a panel that will be shown on multiple pages. The panel’s template requires a few specific queries to be passed to it through the context. The pages that contain the panel don’t require those context variables for anything else. If I include the panel template with the
includetag, I would have to write those queries in every view that contains the panel and pass them as context variables.Alternatively, I could write a custom inclusion tag that contains the queries and passes them to the panel’s template. By using the custom inclusion tag I wouldn’t need to repeat the code to produce its context in every view that contains the panel. My views would contain less code and would be less cluttered with context variables only used by the panel.
Although you are correct in the sense that a custom inclusion tag that simply passes on the context unmanipulated would be the same as the
includetag.