request.POST[]与request.POST.get()的区别

版权声明:内容版权为CSDN用户:kayseen 所有,若您需要引用、转载,需要注明来源及原文链接 https://blog.csdn.net/m0_43394876/article/details/88371421

request.POST是用来接受从前端表单中传过来的数据,比如用户登录过程中传递过来的username、passwrod等字段。

我们在后台进行数据获取时,有两种方法(以username为例):request.POST[‘username’]与request.POST.get(‘username’),
那么这两者有什么不同之处呢?

如果传递过来的数值不为空,那么这两种方法都没有错误,可以得到相同的结果。

但是如果传递过来的数值为空,那么request.POST[‘username’]则会提示Keyerror错误,而request.POST.get(‘username’)则不会报错,而是返回一个none。

简单来说:
request.POST[‘username’]就算没有值也不会报错,返回none
request.POST.get(‘username’)取不到值就会报错,所以推荐使用

猜你喜欢

转载自blog.csdn.net/m0_43394876/article/details/88371421