我想从Python的KeyError
异常中获取键名:
例如:
myDict = {'key1':'value1'}
try:
x1 = myDict['key1']
x2 = myDict['key2']
except KeyError as e:
# here i want to use the name of the key that was missing which is 'key2' in this example
print error_msg[missing_key]
我已经尝试过这个了
print e
print e.args
print e.message
我的代码在Django视图中!
例如,如果我使用ipython并尝试e.arg或e.message,它可以正常工作。但是当我在Django视图中尝试时,我会得到以下结果:
"Key 'key2' not found in <QueryDict: {u'key1': [u'value']}>"
("Key 'key2' not found in <QueryDict: {u'key1': [u'value']}>",)
Key 'key2' not found in <QueryDict: {u'key1': [u'value']}>
虽然我只想要“key2”