python - GAE: Routes vs standard -
in last days saw lot of codes, gae boilerplate, , using routes manage page calls. , i'm wondering why? official examples uses "normal" method:
app = webapp2.wsgiapplication([('/', mainpage), ('/lang', changelanguage)], debug=true)
but discovered alternatives:
from webapp2_extras.routes import redirectroute redirectroute('/lang/<lang>', changelanguage, name='lang', strict_slash=true), redirectroute('/', mainpage, name='home', strict_slash=true)
in first case, i'm using parameters pass vars on request (like /lang?hl=en_us
) , in second must pass path (like /lang/en_us
).
but why use 1 method or other? there advantage?
also, notice first method forms can called, in , put methods, e.g., /register
, routes, call same works, when post done, works if form action /register/
(whith last slash).
one of primary features webapp2
introduced on original webapp
precisely extension of functionality route class provides.
the documentation gives pretty explanation of attempts achieve:
webapp2 introduces routing mechanism extends webapp model provide additional features:
uri building: registered routes can built when needed, avoiding hardcoded uris in app code , templates. if change route definition in compatible way during development, places use route continue point correct uri. less error prone , easier maintain.
keyword arguments: handlers can receive keyword arguments matched uris. easier use , more maintanable positional arguments.
nested routes: routes can extended match more request path. see below route class can match domains , subdomains.
bottom line, they're more powerful version of routing, providing more features programmer.
as per specific parameter question, don't have pass lang
specific way. in first case, lang
available part of request.get
, in second, positional argument request handler's method matches request method (get
, post
).
the difference largely in case of /lang?hl=en_us
, argument technically optional. request still matched handler when argument absent, therefore have verify request.get
contains data.
in second case, /lang/en_us
, route matched, , handler called when there's match in place of <lang>
.
as per slashes issue, you're using strict_slash
in routes. more here
Comments
Post a Comment