You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模板语法错误:bootstrap_field参数传递顺序异常求助

问题解决:Django bootstrap_field模板语法错误

错误原因分析

触发TemplateSyntaxError的核心问题有三个:

  1. 参数拼写错误:show_lable=False里的lable拼写错误,正确应为show_label
  2. 标签参数格式错误:第二处{% bootstrap_field field form.password %}完全不符合语法,应直接传递表单字段form.password,多余的field是无效参数
  3. 表单属性错误:<form action="POST">写法错误,表单提交方式应使用method属性而非action

修正后的完整模板代码

{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content%}

<div class="container-fluid mt-5">
    <div class="d-flex justify-content-center">
        <div class="col-lg-4">
            <div class="card">
                <div class="card-body">
                    <h4 class="text-center text-uppercase mb-3">
                        <b>
                            {% if request.GET.next != '/courier/'%}
                            Customer
                            {% else %}
                            Courier
                            {% endif %}
                        </b>
                    </h4>
                    <form method="POST">
                        {% csrf_token %}
                        {% bootstrap_form_errors form %}
                        {% bootstrap_label "Email" %}
                        {% bootstrap_field form.username show_label=False placeholder="Email" %}
                        {% bootstrap_field form.password %}
                        <button class="btn btn-warning btn-block">Sign in</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{% endblock %}

关键修改点说明

  • 修正show_lable为show_label,解决拼写错误导致的参数识别问题
  • 删除第二处bootstrap_field标签里多余的field参数,改为正确的{% bootstrap_field form.password %}
  • 把表单的action="POST"改为method="POST",符合HTML表单提交规范
  • 移除placeholder ="Email"里的空格,改为placeholder="Email",保持语法严谨性
  • 给justify-content-center所在的div添加d-flex类,确保居中布局生效

内容的提问来源于stack exchange,提问作者mick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:40:32