Django: required field even with true=blank on form submision
(P)I've a form that you have a field'instruction that should be optional to complete.I've tried to model it in the model and put it as a blank field,null true,and even use a default(……)。(p)(P)说明=Models.Charfield(Max U Length=200,blank=true,null=true,default=)(p)(P)However,in submission I'm ask to complete this field.Why?(p)(P)字母名称(p)(P)模特儿(p)字母名称(P)Forms.py(p)字母名称(P)HTML:(p)(P){ fnMicrosoft YaHei}CSRF%(p)字母名称
尝试将其添加到类meta下的表单中:
1 2 3 | def __init__(self, *args, **kwargs): super(StepThreeForm, self).__init__(*args, **kwargs) self.fields['instrucciones'].required = False |
还可以使用此方法向字段添加其他属性:
1 2 3 4 5 | def __init__(self, *args, **kwargs): super(StepThreeForm, self).__init__(*args, **kwargs) self.fields['instrucciones'].required = False self.fields['instrucciones'].label = 'Instrucciones' self.fields['instructions'].help_text = 'Give instructions.' |
您甚至可以使用它来获取表单中的请求:
1 2 3 | def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super(StepThreeForm, self).__init__(*args, **kwargs) |
但您还必须将请求传递给视图:
1 2 3 4 | def get_form_kwargs(self): kwargs = super(ViewName, self).get_form_kwargs() kwargs['request'] = self.request return kwargs |