Forms ValidationError and error code

后端 未结 2 861
我在风中等你
我在风中等你 2021-01-03 22:09

In Django documentation https://docs.djangoproject.com/en/dev/ref/forms/validation/#raising-validationerror said that it is good practice to prodive error code while raising

2条回答
  •  被撕碎了的回忆
    2021-01-03 22:33

    Take a look at ValidationError definition in django src, it's used as a convenient way to pass additional identifier (similar to e.errno in standard python exception), you can use it like this:

    try:
        ...
        raise ValidationError(u'Oops', code=0x800)
        ...
    
    except ValidationError as e:
        print "Error code: ", e.code
    

提交回复
热议问题