Search code examples
pythongoogle-app-engineblobstore

Google App Engine: 405 Method GET is not allowed for this resource


I'm stuck at a point to implement the upload function in my web application using GAE. After submitting at /signup page, it redirects to /upload_file page, while it prompts the error 405 Method Get not allowed, and I was expecting to see the upload form.

(Got some Reference from: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/blobstore/main.py)

Appreciate any help!

Part of the code in a main python script:

class FileUploadFormHandler(BaseHandler): 
# BaseHandler is a subclass of webapp2.RequestHandler.
    def get(self):
        # create an upload URL for the form that the user will fill out
        upload_url = blobstore.create_upload_url('/upload_file')

        self.render('upload-file.html', upload_url = upload_url)

class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):

        upload = self.get_uploads('file')[0]  ## 'file' is a var in the upload-file.html
        blob_key = upload.key()
        blob_reader = blobstore.BlobReader(blob_key) # instantiate a BlobReader for a given BlobStore value.
        locations = parsefile(blob_reader)
        img_url = generate_url(locations=locations)
        self.redirect('/view_map/%s' % img_url)

app = webapp2.WSGIApplication([('/', Home),
                           ('/signup', Register),
                           ('/login', Login),
                           ('/logout', Logout),
                           ('/upload_file', FileUploadHandler),
                           ('/view_map/([^/]+)?', ViewMap)
                           ],
                          debug=True)

Solution

  • After submitting at /signup page, it redirects to /upload_file page, while it prompts the error 405 Method Get not allowed, and I was expecting to see the upload form.

    I think herein lies the problem. After sign up you're redirecting to /upload_file page which is mapped to FileUploadHandler, as follows: ('/upload_file', FileUploadHandler),. Now note that FileUploadHandler has no get method to handle your redirect.

    What I reckon you wanted to achieve is to render your upload_file template(which contains your upload form) after signup and you already have the logic setup in class FileUploadFormHandler. So you should instead map the /upload_file route to FileUploadFormHandler

    Also you will need a route to handle the call to your FileUploadHandler when creating the upload url for the form the user will fill.

    such as:

    class FileUploadFormHandler(BaseHandler): 
    
        def get(self):
            # upload_url handles the POST call to FileUploadHandler
            upload_url = blobstore.create_upload_url('/upload')
            self.render('upload-file.html', upload_url = upload_url)
    

    ...

    app = webapp2.WSGIApplication(
        [('/upload', FileUploadHandler),
         ('/upload_file', FileUploadFormHandler),
        ], debug=True)
    

    NOTE: You should probably come up with less confusing url paths ;)