I would like to share a template between AJAX and regualr HTTP calls, the only difference is that one template needs to be served with the base.html html, the other one without.
Any idea?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The other answers require you to pass an additional context variable. But as long as you can access the request object, there is no need:
{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}
I found this to be much more convenient.
Method 2
Use a variable.
{% extends base_template %}
and in your view, set it to “base.html” in your view, or a new “ajax.html” file which just provides the block and nothing else.
Method 3
{% extends override_base|default:'base.html' %}
P.s. I know this is an old question, but I found it when searching for an answer. Maybe it’ll help someone else with the same problem.
Method 4
You can use {% extends variable %}
Pass a variable base template name in when you create the context in the view.
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#extends
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0