Why is `request.files` empty?

I am trying to follow this simple tutorial for filing files in a flask. I use the following HTML form to submit files:

<form action="" method="post" "enctype=multipart/form-data"> <table border="0" summary=""><tbody> <tr> <th> <label for="image_file">Your image:</label> </th> <td> <input type="file" id="image_file" name="image_file"></input> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="Submit" /> <a href="/test_func">Cancel</a> </td> </tr> </tbody></table> </form> 

After submitting the form, I check the contents of the request variable as follows:

 print 'request.method', request.method print 'request.args', request.args print 'request.form', request.form print 'request.files', request.files 

As a result, I get the following:

 request.method POST request.args ImmutableMultiDict([]) request.form ImmutableMultiDict([('image_file', u'badge.gif')]) request.files ImmutableMultiDict([]) 

What I don't understand is that request.files empty. According to the tutorial above, I need to use file = request.files['file'] to get the file object (to save it).

What am I doing wrong?

+5
source share
1 answer

You have an invalid form tag. The tutorial will tell you:

 <form action="" method=post enctype=multipart/form-data> 

(without quotes), but better-formed HTML will use:

 <form action="" method="post" enctype="multipart/form-data"> 

Note that quotation marks are only associated with attribute values.

Without configuring enctype correctly, the form uses a different encoding, and your browser will not load the file data correctly, and Flask will not try to parse the file data.

+8
source

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


All Articles