Django为什么显示KeyError?

时间:2019-03-01 16:38:03

标签: python django

我一直在看Django文档,了解有关如何将CSS类添加到表单输入模型的示例。但是,当我使用该解决方案时,Django会引发一个KeyError,并且我无法真正查明源代码,因为调试屏幕显示的代码行是模板中完全不相关的CSS类。

解决方案:

#include "ov5642_Sensor_Values.h"

extern const struct sensor_reg ov5642_1280x960_RAW[] =
{
    {0x3103,0x93},
    {0x3008,0x02},
    {0x3017,0x7f},
    .....
};

错误消息:

def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['title', 'content', 'category'].widget.attrs.update({'class': 'form-control'})
        self.fields['content'].widget.attrs.update(size='100', height='50')

谢谢!

1 个答案:

答案 0 :(得分:1)

您不能以这种方式使用多个键:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Fri Mar 01 2019 22:10:42 GMT+0530, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:320:98)
    at configFromString (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2368:15)
    at configFromInput (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2594:13)
    at prepareConfig (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2577:13)
    at createFromConfig (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2544:44)
    at createLocalOrUTC (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2631:16)
    at createLocal (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2635:16)
    at hooks (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:12:29)
    at Object.<anonymous> (E:\workspace_javascript\testingProject\index.js:58:26)
    at Module._compile (internal/modules/cjs/loader.js:688:30)

您将不得不分别查找它们:

self.fields['title', 'content', 'category']

或者在您的代码中:

self.fields['title']
self.fields['content']
...

请注意,在Python的字典中允许使用元组作为键:

for key in ['title', 'content', 'category']:
    self.fields[key].widget.attrs.update({'class': 'form-control'})