查了一下,是因为获取前台数据时,用了request.POST[],改用request.POST.get()之后没有这个报错了

细节:

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

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

如果传递过来的数值不为空,那么这两种方法都没有错误,可以得到相同的结果。但是如果传递过来的数值为空,那么request.POST[‘username’]则会提示Keyerror错误,而request.POST.get(‘username’)则不会报错,而是返回一个none。举例来说:

try:
x = request.POST['sth']
except KeyError:
x = None
x = request.POST.get('sth')

以上两种写法是等价的。

如果数值不存在的话,我们可以指定显示的默认值:

x = request.POST.get('sth', 'default_value')

这里我们可以拿字典对象来进行理解:

list_test = {'a': 1, 'b': 2}

list_test.get('a') # 得到结果1
list_test.get('c') # 得到结果none
list_test.get('c', 3) # 得到设定的默认值3
list_test['b'] # 得到结果 2
list_test['c'] # 返回一个Keyvalue 错误类型

参考:https://stackoverflow.com/questions/12518517/request-post-getsth-vs-request-poststh-difference

django:multivaluedictkeyerror错误的更多相关文章

  1. PyCharm 开发Django ,错误汇总

    近期略微接触了一下Django.在学习的过程中可谓是坎坎坷坷,遇到了很多的问题. 下面就来谈一谈我对Django的一点点的见解. Django项目的创建 使用PyCharm来开发Django项目是非常 ...

  2. python django连接错误

    提示: connection.alias,django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0 ...

  3. django 单元测试错误总结

    TestCase django自带有一个TestCase模块来进行测试,我们可以参考官网 来写单元测试的代码.我这里主要是总结一些错误. 用户无法登陆 我们有些api登录后才可以进行测试,所以我们可以 ...

  4. django自定义错误响应

    在做一个web时,总是会出现各种错误,如400.403.404.500等.一般开发都要做对应的处理,给一些友好提示,或返回一些公益广告等. 在Django中,默认提供了常见的错误处理方式,比如: ha ...

  5. django 事务错误 -- Transaction managed block ended with pending COMMIT/ROLLBACK

    Request Method: GET Request URL: http://192.168.128.111:8000/×××/××××/ Django Version: 1.4.8 Excepti ...

  6. django 编码错误

    估计这个问题是2.7的问题3.0好像就统一utf编码了 报错代码: python :ascii codec can't decode byte 0xe8 in posi 当django中报这个错误的时 ...

  7. django学习错误笔记

    1.运行python manage.py makemigrations polls 出现错误

  8. Django密码错误报错提醒

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOwAAAIBCAYAAABKllNhAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw

  9. django 启动错误:Generator expression must be parenthesized 错误信息:

    错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...

随机推荐

  1. mysql用一个表更新另一个表的方法

    Solution 1:  修改1列(navicate可行) update student s, city c set s.city_name = c.name where s.city_code = ...

  2. Django中的Model、Form和ModelForm

    一  Model(数据库操作) 1 数据表操作 两种类型: Code First:创建类→自动生成表 DB First:创建表→自动生成类 (很少用) 单表操作: 一对多:(Forienkey) 多对 ...

  3. HyberLedger Fabric学习(4)-chaincode学习(操作人员)

    参考:http://hyperledger-fabric.readthedocs.io/en/latest/chaincode4noah.html chaincode也能被称作“智能合约”,一般情况下 ...

  4. curl获取响应时间

    1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间curl -o /dev/nu ...

  5. How To Manually Install Oracle Java on Ubuntu

    Introduction Java is a programming technology originally developed by Sun Microsystems and later acq ...

  6. 迷你MVVM框架 avalonjs 0.9发布

    本版本最大的改进是引进了ms-with绑定,现在可轻松遍历对象了. 改进列表如下: 重新使用082的scanNodes方法,因为有关旧式IE下UI渲染锁死的问题已经解决了. 优化each绑定与Coll ...

  7. 创建maven版的java工程

    步骤如下: 1.第一步 2.第二步: 3.第三步:

  8. MyBatis 延迟加载 加载时机

  9. ConcurrentDictionary内部机制粗解

    ConcurrentDictionary是线程安全类,是什么在保证? 内部类 private class Tables { internal readonly Node[] m_buckets; // ...

  10. Python Modules

    [Python Modules] 1. a module is a python source file. 2. a package is a directory with a __init__.py ...