在模板标签中包含 bootstrap 模态模板

include bootstrap modal template with template tags

我正在尝试创建一个可以包含在我的 html 文件中的 bootstrap 模态,因为我将表单放在模态中并且我不想每次都创建一个新的 mdoal .

它工作正常,除了试图包含表单。这当然是重要的部分。如何在模板标签中包含模板上下文变量?

modal_base

 {% load crispy_forms_tags %}
<div id="{{ modal_form_id }}" class="modal fade" ariahidden="True">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a class="close" data-dismiss="modal">X</a>
                <h1 style="text-align:center;">
                    {{ modal_form_title }}
                </h1>
            </div>
            <div class="modal-body">
                <form method="{{modal_form_method|default:'POST'}}" action="{{modal_form_action}}">
                    {% csrf_token %}
                    {{ form|crispy }}
                    <input class="btn btn-primary" type="submit" value="Create"/>
                </form>
            </div>
            <div class="modal-footer">
                <a href="#" class="btn btn-danger" data-dismiss="modal">
                    Cancel
                </a>
            </div>
        </div>
    </div>
</div>  

示例包含在某些 html 文件中:

 {% include 'modal_form.html' with modal_form_id="new-category" modal_form_title="Create a New Category" modal_form_method="POST" modal_form_action="/home/" form={{category_form}} %}

form = {{category_form}} 是问题所在。有办法解决这个问题吗? 模态呈现良好,打开关闭等我无法通过表单

你很接近!唯一缺少的是您不需要花括号将其包含在 include 标记中

{% include 'modal_form.html' with modal_form_id="new-category" modal_form_title="Create a New Category" modal_form_method="POST" modal_form_action="/home/" form=category_form %}