【问题标题】:Google App Engine: 405 Method GET is not allowed for this resourceGoogle App Engine:405 Method GET is not allowed for this resource
【发布时间】:2016-08-08 02:30:00
【问题描述】:

我在使用 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)

【问题讨论】:

    标签: python google-app-engine blobstore


    【解决方案1】:

    在/signup页面提交后,重定向到/upload_file页面,同时提示错误405 Method Get not allowed,我期待看到上传表单。

    我认为这就是问题所在。注册后,您将重定向到映射到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 路径;)

    【讨论】:

    • 感谢您提供快速而有用的建议! bolbstore 示例代码有效吗?否则,它应该被更新。 :)
    • 你可能指的是哪个例子?
    • 还没有运行它,但我相信那里的信息是正确且最新的,而且它已经完成了它应该做的事情。我认为您似乎对您的程序的逻辑有点困惑
    • 现在,在提交文件后,一个新的问题是“405 POST Method is not allowed”。比较混乱,因为在 FileUploadHandler() 中,post() 方法应该是直接获取上传的文件,(类似于参考示例)。
    猜你喜欢
    • 2016-05-12
    • 2012-01-08
    • 2011-09-25
    • 2017-09-30
    • 1970-01-01
    • 2013-03-21
    • 2016-04-12
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多