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

Django中Bootstrap表单无法以POST提交数据的修改方案咨询

解决Django表单POST提交失败的问题

这跟Bootstrap没关系,问题出在两个核心点:

  • 没有用<form>标签包裹所有表单元素并指定POST提交方法,浏览器默认会采用GET方式提交数据
  • 按钮类型设置为button,这类按钮仅作为普通交互按钮,不会触发表单的提交动作

修改步骤

  1. 添加<form>标签并指定POST方法
    用<form>标签把{% csrf_token %}、输入框、按钮等所有表单相关内容包裹起来,同时设置method="POST"。如果要提交到当前视图,action属性可以留空。

  2. 修改按钮类型为submit
    将按钮的type="button"改为type="submit",只有这类按钮才会触发表单按照指定的POST方法提交数据。

修改后的完整代码示例

<form method="POST" action="">
    {% csrf_token %}
    <div class="form-group">
        <label class="form-label" for="form2Example1">User name</label>
        <input type="text" id="form2Example1" class="form-control" name="username" required/>
    </div>
    <div class="form-group">
        <label class="form-label" for="form2Example2">Password</label>
        <input type="password" id="form2Example2" class="form-control" name="password" required/>
    </div>
    <button type="submit" class="btn btn-primary btn-block mb-4">Sign in</button>
</form>

额外提醒:还要确保对应的Django视图函数中添加了POST请求的处理逻辑,这样才能正确接收和处理提交的数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:20:25