简体   繁体   中英

request handlers and the app.yaml in GAE

I am a newborn baby programmer and have found I don't understand a few things about GAE.

I have my app.yaml setup to route to separate apps

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /unit3.*
  script: unit3.app

- url: /birthday.*
  script: birthday.app

- url: /signup.*
  script: signup.app

- url: /rot13.*
  script: rot13.app

- url: .*
  script: main.app

and then inside signup.app - the WSGI redirects people to a welcome page after a simple post request

import webapp2
import jinja2
import os
import re

template_dir=os.path.join(os.path.dirname(__file__), 'templates')
jinja_env= jinja2.Environment(loader= jinja2.FileSystemLoader(template_dir), autoescape = True)

USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
    return username and USER_RE.match(username)

PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):
    return password and PASS_RE.match(password)

EMAIL_RE  = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
    return not email or EMAIL_RE.match(email)

def render_str(template,**parms):
    t = jinja_env.get_template(template)
    return t.render(parms)

class  BaseHandler(webapp2.RequestHandler):
    """ a BaseHandler object to render and write  """
    def render(self, template, **kw):
        self.response.out.write(render_str(template, **kw))

    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)


class SignUpHandler(BaseHandler):

    def get(self):
        self.render('signup.html')

    def post(self):
        have_error=False
        username=self.request.get('username')
        password=self.request.get('password')
        verify=self.request.get('verify')
        email=self.request.get('email')

        params = dict(username = username, 
                      email = email)

        if not valid_username(username):
            params['name_error']='that is not a valid name'
            have_error=True

        if not valid_password(password):
            params['password_error']=('that is not a valid password')
            have_error=True
        elif password != verify:
            params['verify_error']='your passwords dont match'
            have_error=True

        if not valid_email(email):
            params['email_error']='that is not a valid email address'
            have_error=True

        if have_error:
            params['message']=('Please type your info in again ' + username)
            self.render('signup.html', **params)
        else:
            self.redirect('welcome?username=' + str(username))

class WelcomeHandler(BaseHandler):

    def get(self):
        username = self.request.get('username')
        if valid_username(username):
            self.render('welcome.html', username = username)
        else:
            self.redirect('signup')


app = webapp2.WSGIApplication([('/signup',SignUpHandler),
                               ('/welcome',WelcomeHandler)]
                               ,debug=True)

Though the WelcomeHandler doesn't find the templated html file, i get a 404.

Basically i get this 404 for anything routed via a RequestHandler.

I am pretty sure this is a basic misunderstanding that I have overlooked and can't easily correct with a google search.

Do I need to router everything in the .yaml? Why is this not the case when i just have generic URL handler?

You need to add the /welcome.* route to your app.yaml so it is served by signup.app .

IN ADDITION , if I make a recommendation. You currently only support the routes /signup and /welcome in signup.app :

app = webapp2.WSGIApplication([('/signup',SignUpHandler),
                               ('/welcome',WelcomeHandler)]
                               ,debug=True)

but you are routing all of /signup.* in app.yaml . So if /signup/ will get sent to this WSGI handler and will result in a 404. Instead of doing this, either add explicit paths in app.yaml and a 404 handler in your catch-all in main.app or add a 404 handler catch-all in each submodule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM