我有一个带有DateField的表单 - 'receivedon'属性如下:
forms.py:
class PreDataForm(forms.ModelForm):
journalname=forms.ChoiceField(required=True,choices=JOURNAL_CHOICES)
articletype = forms.ChoiceField(required=True, choices=ARTICLE_TYPES)
granttype = forms.ChoiceField(required=True, choices=GRANTS)
receivedon=forms.DateField(widget=forms.SelectDateWidget)
class Meta:
model=PreData
fields=['journalname','articlename','articlenumber','caname','camail','articletype',
'country','affliation','granttype','status','receivedon']
我将要求用户在html中填写该日期,正常程序:
HTML:
<div class="center col-md-6">
Received On:
{{ form.receivedon }}
</div>
现在我想通过像这样的django视图上传日期:
views.py:
if request.method=="POST":
form=PreDataForm(request.POST or None)
print request.POST['receivedon']
if form.is_valid():
instance=form.save(commit=False)
instance.save()
有一点是,当我检查post
函数时,表单始终无效is_valid
请求,当我尝试使用datefield
显示request.POST['receivedon']
数据时,其他内容我是获得多值Dict键错误
任何人都能帮助我吗?感谢。
追溯 - 编辑:
File "/Users/Devadanam/Desktop/django_project_postgresql/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Users/Devadanam/Desktop/django_project_postgresql/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Devadanam/Desktop/django_project_postgresql/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/Devadanam/Desktop/django_project_postgresql/django_project/jsmmetrics/views.py" in createdata
55. print request.POST['receivedon']
File "/Users/Devadanam/Desktop/django_project_postgresql/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__
85. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /metrics-jsm/create-data/
Exception Value: "'receivedon'"
答案 0 :(得分:0)
您未在receivedon
收到密钥request.POST
。您可以执行以下操作以避免此特定键出现此错误:
request.POST.get('receivedon', <some default value>)