Kristian Nissen

Icon

my contemporary identity

Fooling around with app engine

As allways, developing anything but MS stuff on your windows platform  causes headaces …I’m running through Using Google App Engine, 1st Edition but the code doesn’t quit take care of windows path problems and in section 6.10. Extending Our Application a general purpose method is introduced to take care of finding a template, assigning values to it and render it like this:

def doRender(handler, tname='index.htm', values={}):
  temp = os.path.join(
      os.path.dirname(__file__),
      'templates/' + tname)
  if not os.path.isfile(temp):
    return False

  # Make a copy of the dictionary and add the path
  newval = dict(values)
  newval['path'] = handler.request.path

  outstr = template.render(temp, newval)
  handler.response.out.write(outstr)
  return True

- which is fine if your on anything but windows! But on windows “templates/” will give you a problem because that’s not where the file is located! If you want to us a general purpose method for finding your template files i a subfolder, use this snippet:

def assign(template):
 return os.path.join(os.path.join(
 os.path.dirname(__file__), 'templates'), template)

Assign may not be the best name, but the method returns a valid path to your template, you can then use it like this:

class HomePage(webapp.RequestHandler):
  def get(self):
    path = assign('home.html')
    self.response.out.write(template.render(path, {}))

But I would suggest adding something more to the method so that the entire call to self.response.out.write can be removed from the get method.

Filed under: code is poetry , , ,

Leave a Reply