Google App Engine:此资源不允许使用 405 方法 GET

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

我在使用 GAE 在我的 Web 应用程序中实现上传功能时卡住了。在/signup页面提交后,重定向到/upload_file页面,同时提示错误405 Method Get not allowed,我期待看到的是上传表单。

(参考资料来自:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/blobstore/main.py

感谢任何帮助!

主要 python 脚本中的部分代码:

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)

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.

我觉得问题就在这里。注册后,您将重定向到映射到 FileUploadHandler/upload_file 页面,如下所示:('/upload_file', FileUploadHandler),。现在请注意,FileUploadHandler 没有 get 方法 来处理您的重定向。

我认为您想要实现的是在注册后呈现您的 upload_file 模板(其中包含您的上传表单)并且您已经在 class FileUploadFormHandler 中设置了逻辑。所以你应该将 /upload_file 路由映射到 FileUploadFormHandler

在为用户填写的表单创建上传 url 时,您还需要一个路由来处理对 FileUploadHandler 的调用。

如:

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)

注意:您应该想出不那么令人困惑的 url 路径 ;)