File upload deformation overrides my error message with Invalid pstruct: not an instance of FieldStorage

If I do not select the file and just click send, I get the following error: -

Invalid pstruct: {'upload': "b'' is not a FieldStorage instance"} 

This is not the behavior that I get to deform the demo site , leaving it empty leads to a more reasonable "Required" error message.

Using my own validator as shown below does not solve the problem: -

 def validate_file(node, value, **kwargs): if not value: raise colander.Invalid(node, "Please select a file") class Schema(colander.MappingSchema): excel_file = colander.SchemaNode(deform.FileData(), widget=deform.widget.FileUploadWidget(tmpstore), validator=validate_file) 

I see that an error occurs, but the output of e.render() , where e is the ValidationFailure from form.validate , does not match the error itself. The corresponding deform source code is in 'widget.py', where the _FieldStorage class checks to see if the cstruct has a cstruct attribute and raises its own Invalid exception.

Here is the function that executes the verification call (a really standard bot) that returns the displayed page.

 def generate_upload_form(request): form = deform.Form(upload_schema, buttons=('submit',)) if getattr(request, 'POST') and 'submit' in request.POST: try: value_dict = form.validate(request.POST.items()) except deform.ValidationFailure as e: # Invalid form form = e.render() else: # Successfully validated, now do operation upload_form_operation(request, value_dict) if isinstance(form, deform.Form): form = form.render() return form 

How can I show my own error message without deactivating the deform codebase code?

+6
source share
1 answer

Are you sure you submitted the form data correctly? Typically, this error occurs when a deformation attempts to deserialize a represented value using duck input.

One specific element that is overlooked is to make sure your HTML form has an extra enctype type, for example.

  enctype="multipart/form-data" 

Without this, the form sends the file name as a string, which then crashes

+1
source

Source: https://habr.com/ru/post/1015705/


All Articles