简体   繁体   English

请求处理程序和GAE中的app.yaml

[英]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. 我是一名刚出生的婴儿程序员,发现我对GAE的一些看法并不了解。

I have my app.yaml setup to route to separate apps 我有app.yaml设置路由到单独的应用程序

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 然后在signup.app内部 - 在简单的帖子请求之后,WSGI将人们重定向到欢迎页面

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. 虽然WelcomeHandler找不到模板化的html文件,但我得到了404。

Basically i get this 404 for anything routed via a RequestHandler. 基本上我通过RequestHandler路由到任何路由器获得404。

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? 我需要将.yaml中的所有内容路由到路由器吗? Why is this not the case when i just have generic URL handler? 当我只有通用的URL处理程序时,为什么不是这种情况?

You need to add the /welcome.* route to your app.yaml so it is served by signup.app . 您需要将/welcome.*路由添加到app.yaml以便它由signup.app

IN ADDITION , if I make a recommendation. 此外 ,如果我提出建议。 You currently only support the routes /signup and /welcome in signup.app : 您目前仅支持signup.app的路由/signup/welcome

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

but you are routing all of /signup.* in app.yaml . 但是您在app.yaml中路由所有/signup.* 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. 因此,如果/signup/将被发送到此WSGI处理程序并将导致404.而不是这样做,要么在app.yaml添加显式路径,在main.app中的catch-all中添加404处理程序,或者添加404处理程序每个子模块都包含所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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