23👍
✅
From the docs:
The template name can either be a
variable or a hard-coded (quoted)
string, in either single or double
quotes.
So construct a variable in your view and pass it to your template via the context, say as ‘modeTemplate’: “page_”+mode+”.html”. Then do:
{% include modeTemplate %}
Assuming ‘mode’ is a python variable in your view code.
12👍
I think that goliney provided a better answer here
In summary:
You can combine django’s built-in ‘with’ template tag to assign a dynamically constructed template name to a variable and the ‘add’ filter and to dynamically construct a template name.
For example:
{% with template_name='page_'|add:mode|add:".html" %}
{% include ""|add:template_name %}
{% endwith %}
- [Django]-How to change help text of a django form field?
- [Django]-The view didn't return an HttpResponse object. It returned None instead
- [Django]-Django: Multiple url patterns starting at the root spread across files
3👍
You can concatenate plain strings and variables right in your templates using with
tag and add
filter,
see this SO answer.
- [Django]-Django: Error: You don't have permission to access that port
- [Django]-How can I get Django to print the debug information to the console?
- [Django]-How do I start up remote debugging with PyCharm?
Source:stackexchange.com