Django提交表单报错'WSGIRequest' object has no attribute 'Post'如何解决
报错原因
你遇到的AttributeError是属性名拼写错误导致的:Django 中WSGIRequest对象存储 POST 请求提交数据的属性名为全大写的POST,你代码中写的request.Post不存在,因此触发异常。
修复方法
将视图函数中打印的代码修改为print(request.POST)即可,修复后的完整视图代码如下:
def profile(request): if request.method == "GET": return render(request, "userprofile/profile.html") elif request.method == "POST": print(request.POST) return render(request, "userprofile/profile.html")
其他优化建议
- 你的HTML表单代码中
<input type="text" placeholder="Weight", name="weight">一行多了多余的逗号,建议删除避免HTML解析异常,修正后写法为<input type="text" placeholder="Weight" name="weight"> - 后续如果需要获取表单单个字段的提交值,推荐使用
request.POST.get('weight')、request.POST.get('reps')的写法,可避免字段不存在时抛出KeyError
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

