Any way to make {% extends '...' %} conditional? - Django

PythonAjaxDjangoDjango Templates

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

{% 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.

Solution 4 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRadiantHexView Question on Stackoverflow
Solution 1 - PythonCruelView Answer on Stackoverflow
Solution 2 - PythonDaniel RosemanView Answer on Stackoverflow
Solution 3 - PythonBrigandView Answer on Stackoverflow
Solution 4 - PythonTom GrunerView Answer on Stackoverflow