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

Django报错IntegrityError:NOT NULL约束失败(sturesidance字段)求助

Django IntegrityError 排查:NOT NULL constraint failed: login_new_user.sturesidance

核心问题出在模板输入框的name属性拼写错误,导致后端无法获取到sturesidance字段的值,最终触发非空约束错误。

错误对比:

  • 模型中定义的字段名:sturesidance=models.TextField()
  • 模板中input的name属性:name="stureisidance"(这里把residance拼成了reisidance,多了一个字母i)

修复步骤:

修改模板中的input标签,将name属性值修正为和模型字段一致的sturesidance:

<!-- residance -->
<div class="input-group mb-2 ">
    <span class="input-group-text w-25" for="residance">Residence</span>
    <input class="form-control text-bg-primary bg-opacity-10 text-dark text-opacity-50"
        type="textarea" name="sturesidance" cols="4" rows="5" placeholder="type current address"
        required>
</div>

验证逻辑:

修改后,表单提交时前端会将该字段的值以sturesidance为键传递给后端,你在创建new_user对象时就能正确获取到对应值,不会再出现字段为null的情况,也就避免了触发NOT NULL constraint failed错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:05:28