html - How to combine multiple handlers into one using python and self.request.path -
i making basic website web design class using python, html, , google app engine. able make website using multiple handlers, supposed make multi page site using 1 handler.
i can handlers such as:
class indexhandler(webapp2.requesthandler): def get(self): template = jinja_environment.get_template('templates/index.html')) self.response.write(template.render({'title': 'home', 'header':'home'})) class friendhandler(webapp2.requesthandler): def get(self): template = jinja_environment.get_template('templates/friends.html') self.response.write(template.render({'title': "friends", 'header': "friends"}))
to work, when try combine them using:
class allthehandlers(webapp2.requesthandler): def get(self): template = jinja_environment.get_template('templates%s' % self.request.path) self.response.write(template.render({'title', 'header'})) outstr = template.render(temp, { }) self.response.out.write(outstr)
i 404 error, , log says:
info 2016-02-06 06:14:13,445 module.py:787] default: "get / http/1.1" 404 154
any appreciated, pointers on how use self.request.path attribute helpful think. feel part of issue has end of code i'm not sure:
app = webapp2.wsgiapplication([ ('/', allthehandlers), # ('/bio.html', allthehandlers), # ('/index.html', allthehandlers), # ('/friends.html', allthehandlers), # ('/horses.html', allthehandlers), ], debug=true)
thanks can provide!
error 404 means requesthandler
not reached. problem in uri routing.
currently, have 1 route configured:
app = webapp2.wsgiapplication([ ('/', allthehandlers),
it doesn't mean /
, under it, may expect. means /
, nothing else.
if want serve multiple simple html templates, can change @ follows:
class allthehandlers(webapp2.requesthandler): def get(self, html_page): template = jinja_environment.get_template('templates/%s' % html_page) # ... app = webapp2.wsgiapplication([ ('/(\w+\.html)', allthehandlers), ], debug=true)
(\w+\.html)
regular expression matches someword.html
. since put in brackets, catched , transferred get()
argument. can choose appropriate template.
disclaimer: code above should not viewed practice - it's illustration of how routing works.
Comments
Post a Comment