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
相关产品推荐
相关产品推荐

