当没有给出输入并且我单击保存按钮时,如何在 NicheSkills 表单的第一个字段中设置验证。我希望在第一行中没有输入时将错误显示为“此字段是必需的”。请帮助我。
模型.py:
class NicheSkills(Audit):
user = models.ForeignKey(User, null = True, blank = True)
title = models.CharField(max_length = 15)
special_skill = models.CharField(max_length = 15)
表格.py:
class NicheSkillsForm(forms.Form):
title = fields.CharField(max_length=150)
special_skill = fields.CharField(max_length=150)
NicheFormset = formsets.formset_factory(NicheSkillsForm, extra = 1)
模板:
<table cellpadding="3" cellspacing="3" border="0" style="margin-left:65px;">
<tbody>
<tr><td>{{ form5.current_role }}</td></tr>
<tr><td><span class = "forget-errors">{{ form5.current_role.errors }}</span></td></tr>
<tr><td align="center"><input type="button" id="form5" name="save" value="Save" onclick = "save_form(this.id)" /></td></tr>
</tbody>
</table>
视图.Py
NicheFormset = formsets.formset_factory(NicheSkillsForm, extra = 0)
if request.method == "POST":
formset6 = NicheFormset(request.POST, prefix='fs6')
if formset6.is_valid():
for values in formset6.cleaned_data:
niche_details = NicheSkills(
title = values['title'],
special_skill = values['special_skill'])
niche_details.save()
else:
NicheFormset = formsets.formset_factory(NicheSkillsForm, extra = 1)
formset6 = NicheFormset(prefix='fs6')
return render_to_response('test.html', locals(), RequestContext(request))