Reputation: 4419
I am working on getting Jinja2 to work with Google AppEngine. I have the following for my main.py code:
import os
import webapp2
import jinja2
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {
'name': 'SomeGuy',
'verb': 'extremely enjoy'
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
webapp2.WSGIApplication([('/', MainPage)], debug=True)
This has been killing me for hours I would be grateful for some help.
UPDATE:
I have changed the code a bit to update the situation. The logs are telling me:
ImportError: <module 'main' from '/base/data/home/apps/s~devpcg/1.359633215335673018/main.pyc'> has no attribute app
and the above code is all from my main.py folder. I have a file index.html in a folder called templates that is in the same directory as the main.py file.
Upvotes: 1
Views: 1585
Reputation: 169264
I was not sure if this is a copy-paste error when pasting your code over to stackoverflow, but you do seem to be getting an indentation error as indicated in the comments...
This is the correct indentation:
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {
'name': 'SomeGuy',
'verb': 'extremely enjoy'
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
Edit:
Based on the new error I would recommend you give a little bit more information about how your application is structured.
I am guessing that you are showing us your main.py
file.
If that is indeed the case you need to have something like the code below in that file (assuming Python 2.7).
For more-granular details please refer to the documentation:
https://developers.google.com/appengine/docs/python/python27/using27#Configure_WSGI_Script_Handlers
app = webapp2.WSGIApplication(routes=[
( r'/', MainPage ),
# ... other paths ...
], debug=True) # True for now until ready for prod...
Upvotes: 3