gpt4 book ai didi

python - Django 表单 : Hidden Field Error When Submitting Form

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:13 25 4
gpt4 key购买 nike

我有一个隐藏字段,可以在创建文章时保存,还有另一个隐藏字段可以告诉文章状态:forms.py:

class SavedArticles(forms.ModelForm):
class Meta:
model = Article
fields =['title', 'content', 'status', 'time_created','publisher']
widgets = {
'status': forms.HiddenInput(),
'time_created': forms.HiddenInput()
}

time_createdstatus的内容将由我在后端提供:

def publish(request):
if request.method == 'POST':
article = SavedArticles(request.POST or None)
if article.is_valid():
article = article.save(commit = False)
article.status = 0 #have tried articles.data['status'] = 0
article.time_created = timezone.now() #have tried articles.data['time_created'] = 0
article.save()
return HttpResponseRedirect(reverse('mysite:home'))

我的路线很好,但每当我感觉表单已提交时,我都会收到以下错误:

(Hidden field status) This field is required.
(Hidden field time_created) This field is required.

我在做什么错误?我一整天都在尝试。

最佳答案

指定 ModelForm 时,如果 POST 数据中存在未在 fields 列表/元组中指定的字段,Django 将尝试根据POST,无论是否未在 ModelForm 上指定字段。

如果没有向表单类提供初始数据,并且表单输入没有值,并且模型上的字段未指定为 blank=True,则表单验证将失败。

关于python - Django 表单 : Hidden Field Error When Submitting Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25896817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com