我想从 PythonKeyError
异常中获取键名:
例如:
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'