Search code examples
pythonrouteswebapp2

Google App Engine: webapp2 routing not working


I created these routes:

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

With these handlers:

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))

But every time I call "empresa/perfil" it returns a 404.

I thought it was trying to reach a method with a parameter named "perfil", but after modifying the response handler I still get the same error.

Am I missing something?

Edit: including 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 

Solution

  • It turns out the app.yaml wasn't catching that route. I modified the following line to fix that:

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

    Thanks to Rafael Barros for his help :)