【问题标题】:Django Include Template based on ContextDjango 包含基于上下文的模板
【发布时间】:2023-04-03 11:13:01
【问题描述】:

我想知道是否有办法在 Django 模板的 include 语句中包含变量。例如,如果您对一组对象有一个 for 循环,并且您只想加载与这些对象相关的模板(示例如下)。

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

或者有没有更好的方法来做到这一点。谢谢。

【问题讨论】:

    标签: django django-templates django-template-filters


    【解决方案1】:

    你可以这样做:

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

    但在这种情况下,我宁愿制作一个模板标签或过滤器来封装这个逻辑远离模板。

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多