Search code examples
pythonhtmlwebapp2

Sending form data in HTML with Python and Google App Engine


My HTML code:

<form action="/set_image" method="post" id="form1" runat="server" enctype='multipart/form-data'>
       <div class="fileButtons">
       <input type='file' id="imgInp" name="imgInp" accept="image/*"/>
        <input type="submit" id="submit" name="action" value="Send"/>
        <input type='button' id='remove' value='Remove' />
        </div>
    </form>

main.py:

class SetImage(webapp2.RequestHandler):
    def post(self):
        id = str(self.request.get('id'))
        image = str(self.request.get('imgInp'))
        if (image):
            set_image(id, image)
            self.response.out.write("Image Uploaded")
        else:
            self.response.out.write("No Image Selected")

I'm able to get the image , but the id is empty when I try to print it.


Solution

  • If you expect to get a post value with the name id, you need to have that as a form element somewhere in your form. Such as:

    <input type="text" name="id" />
    

    However, I would not suggest using id as a name for a form element as it might cause name clashes.

    If you are trying to get the name of the file, that value is probably stored in:

    id = self.request.POST.get('imgInp')