Django基于上下文包含模板



我想知道是否有一种方法在django模板中包含一个变量。例如,如果您在一组对象上有一个循环,并且只想加载与这些对象相关的模板(例如bellow)。

{% for app in apps %}
<div class="modal fade hidden" id="myModal_{{ app.id }}">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Add {{ app.title }}</h3>
  </div>
  <div class="modal-body">
    <form class="bs-docs-example form-horizontal">

      {% include "app_templates/{{ app.title }}/modal-config.html" %}

    </form>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="add-app-btn btn btn-primary" data-appid="{{ app.id }}">Add</button>
  </div>
</div>
{% endfor %}

或这样做的更好方法。谢谢。

您可以这样做:

{% with template_name="app_templates/"|add:app.title|add:"/modal-config.html" %}
    {% include template_name %}
{% endwith %}

我不确定这甚至可以起作用:

{% include "app_templates/"|add:app.title|add:"/modal-config.html" %}.

但是,在这种情况下,我宁愿制作一个模板标签或过滤器以将此逻辑封装在模板上。

相关内容

最新更新