0

I am currently making a very basic website for my web design class using python, html, and google app engine. I am able to make the website using multiple handlers, but we are supposed to make a multi page site using only 1 handler.

I can get 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, but when I try to 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 get a 404 error, and my log says:

INFO     2016-02-06 06:14:13,445 module.py:787] default: "GET / HTTP/1.1" 404 154

any help would be very much appreciated, even just pointers on how to use the self.request.path attribute would be helpful I think. I feel like part of my issue has to do with the end of my code but I'm not sure:

app = webapp2.WSGIApplication([
    ('/', AllTheHandlers),
    # ('/bio.html', AllTheHandlers),
    # ('/index.html', AllTheHandlers),
    # ('/friends.html', AllTheHandlers),
    # ('/horses.html', AllTheHandlers),
], debug=True)

thanks for any help you can provide!

1 Answers1

1

Error 404 means that your RequestHandler is not even reached. Problem is in URI routing.

Currently, you only have one route configured:

app = webapp2.WSGIApplication([
    ('/', AllTheHandlers),

It doesn't mean / and everything under it, as you may expect. It means / and nothing else.

If you want to serve multiple simple html templates, you can change it at 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) is a regular expression that matches someword.html. Since we put it in brackets, it is catched and transferred to get() as an argument. Then we can simply choose appropriate template.


DISCLAIMER: The code above should not be viewed as a good practice - it's just an illustration of how routing works.

alexanderlukanin13
  • 4,577
  • 26
  • 29
  • ok, I understand the parts about the error and only having one route configured, but I know I'm supposed to use the self.request.path. I can access a page, but now it tells me: TemplateNotFound: templates/ and I'm not really sure how to move forward with this. Its not realizing that it should be using my index.html file as a home page. I think a lot of this just stems from the fact that I don't understand self.request.paths any clarification would be great thanks! – Mara Varady Feb 07 '16 at 20:51