我一直坚持使用GAE在我的网络应用程序中实现上传功能。在/ signup页面提交后,它会重定向到/ upload_file页面,同时它会提示错误405 Method Get不允许,我希望看到上传表单。
(参考文献: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)
答案 0 :(得分:-1)
在/ signup页面提交后,它会重定向到/ upload_file页面,同时它会提示错误405方法获取不被允许,我希望看到上传表单。
我认为这就是问题所在。注册后,您将重定向到/upload_file
页面,该页面已映射到FileUploadHandler
,如下所示:('/upload_file', FileUploadHandler),
。现在请注意,FileUploadHandler
没有获取方法来处理您的重定向。
我想要实现的是在注册后呈现您的upload_file
模板(其中包含您的上传表单),并且您已经在class FileUploadFormHandler
中设置了逻辑设置。因此,您应该将/upload_file
路线映射到FileUploadFormHandler
在为用户填写的表单创建上传网址时,您还需要一条路由来处理对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路径;)