如果传递过来的数值不为空,那么这两种方法都没有错误,可以得到相同的结果。
但是如果传递过来的数值为空,
那么request.POST[‘title’]则会提示Keyerror错误,
而request.POST.get(‘title’)则不会报错,而是返回一个none。
简单来说:
request.POST[‘title’]就算没有值也不会报错,返回none
request.POST.get(‘title’)取不到值就会报错,所以推荐使用
建议使用
title = request.POST.get(“title”, " "),为其自定义一个默认值