June 14, 2008

Simple Google App Engine Template - Simple Example

I was having some problems with Google App Engine's Template - I kept getting the following message:

global TemplateDoesNotExist = <class 'django.template.TemplateDoesNotExist'>, name = 'x.html'
<class 'django.template.TemplateDoesNotExist'>: x.html

I was sure my code was right - somehow it was not reading new files. The solution is was simply to stop and restart my Google application in the app engine console.

Here is my simple example - followed by a more complex simple example:

app.yaml snippet:

handlers:
- url: /
script: helloworld.py

helloworld.py:

import os
from google.appengine.ext.webapp import template

print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
print template.render('x.html', {'a': 'b' })

x.html:

Hello there in my template

The value of a is {{ a }}

---------------------
Note that when this simple example runs - it does not process HTML - somehow Google App Engine is escaping everything - I am guessing this the nature of "print".

So here is a more complex example - effectively a trivial way to get your index.html to be a template that welcomes folks.
---------

app.yaml snippet:

handlers:
- url: /
script: index.py

index.py:

#!/usr/bin/env python

import os
import cgi
import wsgiref.handlers
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
import django.template

class MainHandler(webapp.RequestHandler):

def get(self):
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, {'a': 'b' }))

def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
main()


index.html:

<h1>Welcome</h1>
<p>The value for a is {{ a }}.</p>

Posted by csev at June 14, 2008 08:50 AM