【问题标题】:Google App Engine: webapp2 routing not workingGoogle App Engine:webapp2 路由不起作用
【发布时间】:2014-11-17 19:39:31
【问题描述】:

我创建了这些路线:

app = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/empresa', Empresa),
  ('/empresa/perfil', EmpresaPerfil),
], debug=True)

使用这些处理程序:

class Empresa(webapp2.RequestHandler):
  def get(self):
    template_values = {}
    template = JINJA_ENVIRONMENT.get_template('templates/empresa/index.html')
    self.response.write(template.render(template_values))

class EmpresaPerfil(webapp2.RequestHandler):
  def get(self):
    template_values = {}
    template = JINJA_ENVIRONMENT.get_template('templates/empresa/perfil.html')
    self.response.write(template.render(template_values))

但每次我调用“empresa/perfil”时,它都会返回 404。

我以为它试图使用名为“perfil”的参数访问方法,但在修改响应处理程序后,我仍然收到相同的错误。

我错过了什么吗?

编辑:包括 app.yaml

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /img
  static_dir: templates/img/
- url: /empresa
  static_dir: templates/empresa/
- url: /estudiante
  static_dir: templates/estudiante/
- url: /css
  static_dir: templates/lib/css/
- url: /js
  static_dir: templates/lib/js/
- url: /templates
  static_dir: templates/
- url: /.*
  script: guestbook.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2                                                                  
  version: latest 

【问题讨论】:

  • 你的 app.yaml 看起来怎么样?
  • 那里,我刚刚添加了它。
  • guestbook.py 文件中的EmpresaEmpresaPerfil 这两个类吗?这是在 app.yaml 中定义的项目的入口点
  • 是的,它们都在 guestbook.py 中。
  • 哦,我完全错过了,app.yaml 中的- url: /empresa 规则优先于处理程序EmpresaPerfil,如果您在templates/empresa/ 文件夹下没有perfil,它将返回404

标签: python routing webapp2


【解决方案1】:

原来 app.yaml 并没有赶上那条路线。 我修改了以下行来解决这个问题:

- url: /empresa/.*
  static_dir: templates/empresa/

感谢Rafael Barros 的帮助:)

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 2019-09-15
    • 2015-08-14
    • 2011-07-27
    • 2017-10-31
    • 2019-11-03
    • 2012-11-15
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多