【问题标题】:Trying to include django form in my base.html, but not rendering in other templates?试图在我的 base.html 中包含 django 表单,但不在其他模板中呈现?
【发布时间】:2013-09-25 15:43:59
【问题描述】:

我正在使用 django haystack 进行搜索,并希望将表单包含在我的 base.html 文件中,以便它显示在我的所有模板中。它会在我加载到我的默认 url 时显示,但我无法在加载结果模板时显示表单标记 {{ form.as_table }}。 html 表单仍然存在,但表单标签不存在。

这都包含在base.html中

<form method="get" action=".">
    <table>
        ##{{ form.as_table }} ## this doesn't show up when loading the results template
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search" class="btn btn-primary">
            </td>
        </tr>
    </table>
</form>

【问题讨论】:

    标签: django django-forms django-haystack


    【解决方案1】:

    可能只有您的主页视图在您的上下文中定义form所有你的观点必须定义它:

    context['form'] = MyFormClass()
    

    【讨论】:

      【解决方案2】:

      您应该使用Template Context Processor。检查这个answer,它对我有用。

      【讨论】:

        猜你喜欢
        • 2013-02-11
        • 2018-12-15
        • 2020-02-05
        • 2013-12-16
        • 2019-11-14
        • 2016-12-06
        • 2019-04-04
        • 1970-01-01
        • 2017-10-01
        相关资源
        最近更新 更多