开发者

django: Class-based Form has no errors but is not valid. What is happening?

I have a form which is returning False from .is_valid(), but .errors and .non_field_errors() appear to be empty. Is there any other way to check out what might be causing this?

In case it's a problem with my logging code, here it is:

logger.debug('form.non_field_errors(): ' + str(form.non_field_errors()))
logger.debug('form.errors: ' + str(form.errors))

My form code:

class IncorporateForm(forms.Form):
        type_choices = (("LTD", "Private company limited by shares"),
                        ("LTG", "Private company limited by guarantee"),
                        ("PLC", "Public limited company"),
                        ("USC", "Unlimited company with share capital"),
                        ("UWS", "Unlimited company without share capital"))

        country_choices = (("EW", "England and Wales"),
                          ("CY", "Wales"),
                          ("SC", "Scotland"),
                          ("NI", "Northern Ireland"))

        articles_choices = (("MOD", "Model articles"),
                            ("AMD", "Model articles with amendments"),
                            ("BES", "Entirely bespoke articles"))

        name = forms.CharField(initial = "[name] limited")

        registered_office = forms.CharField(widget=forms.Textarea,
                                            label='Registered office address')

        registration_country = forms.ChoiceField(choices=country_choices,
                                                 widget=forms.RadioSelect(renderer=SaneRadioField))

        company_type = forms.ChoiceField(choices=type_choices,
                                         widget=forms.RadioSelect(renderer=SaneRadioField), initial="LTD")

        articles_type = forms.ChoiceField(choices=articles_choices,
                                          initial='MOD',
                                          widget=forms.RadioSelect(renderer=SaneRadioField))

        restricted_articles = forms.BooleanField()

        arts_upload = forms.FileField(label='Articles to upload')

My view code (to the point where I detect that the form is not valid):

def incorporate_view(request):
        form = IncorporateForm()
        DirectorsFormset = forms.formsets.formset_factory(OfficerForm, extra=30)
        CapitalFormset = forms.formsets.formset_factory(CapitalForm, extra=30)
        HoldingFormset = forms.formsets.formset_factory(HoldingForm, extra=30)
        AmendsFormset = forms.formsets.formset_factory(ArticlesAmendsForm, extra=50)

     开发者_StackOverflow   if request.method == 'POST':
                #bind and validate
                form.data = request.POST
                guarantee_form = GuaranteeForm(data=request.POST)
                directors_formset = DirectorsFormset(prefix='directors', data=request.POST)
                capital_formset = CapitalFormset(prefix='capital', data=request.POST)
                holding_formset = HoldingFormset(prefix='holding', data=request.POST)
                amends_formset = AmendsFormset(prefix='amends', data=request.POST)
                save_objects = [] # objects to be saved at the end if there is no error
                user_objects = {} # keyed by email
                individual_objects = {} # keyed by email?

                if(not (form.is_valid() and guarantee_form.is_valid()
                        and directors_formset.is_valid()
                        and capital_formset.is_valid() and
                        holding_formset.is_valid() and
                        amends_formset.is_valid())):
                        dbg_str  = """
                        form.is_valid(): %s
                        guarantee_form.is_valid(): %s
                        directors_formset.is_valid(): %s
                        capital_formset.is_valid(): %s
                        holding_formset.is_valid(): %s
                        amends_formset.is_valid(): %s
                         """ % (form.is_valid(), guarantee_form.is_valid(),
                                directors_formset.is_valid(),
                                capital_formset.is_valid(),
                                holding_formset.is_valid(),
                                amends_formset.is_valid())
                        logger.debug(dbg_str)
                        logger.debug('form.non_field_errors(): ' + str(form.non_field_errors()))
                        logger.debug('form.errors: ' + str(form.errors))


Assigning to form.data does not bind the form — you should pass the data dict when the object is constructed (or look into the code and see which flags are set, but that's probably not documented and therefore not recommended).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜