Inject errors into already validated form?

后端 未结 4 1349
梦如初夏
梦如初夏 2020-12-12 15:55

After my form.Form validates the user input values I pass them to a separate (external) process for further processing. This external process can potentially fi

4条回答
  •  情歌与酒
    2020-12-12 16:26

    Form._errors can be treated like a standard dictionary. It's considered good form to use the ErrorList class, and to append errors to the existing list:

    from django.forms.utils import ErrorList
    errors = form._errors.setdefault("myfield", ErrorList())
    errors.append(u"My error here")
    

    And if you want to add non-field errors, use django.forms.forms.NON_FIELD_ERRORS (defaults to "__all__") instead of "myfield".

提交回复
热议问题