1

我正在努力让 Jinja2 与 Google AppEngine 一起工作。我的 main.py 代码有以下内容:

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)

这已经杀死了我几个小时,我将不胜感激。

更新:

我已经稍微更改了代码以更新情况。日志告诉我:

ImportError: <module 'main' from '/base/data/home/apps/s~devpcg/1.359633215335673018/main.pyc'> has no attribute app

上面的代码都来自我的 main.py 文件夹。我在名为 templates 的文件夹中有一个文件 index.html,它与 main.py 文件位于同一目录中。

4

1 回答 1

3

在将代码粘贴到 stackoverflow 时,我不确定这是否是复制粘贴错误,但您似乎确实收到了评论中指示的缩进错误...
这是正确的缩进:

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))

编辑:
根据新错误,我建议您提供更多有关您的应用程序结构的信息。
我猜你是在向我们展示你的main.py文件。
如果确实是这种情况,您需要在该文件中包含类似于以下代码的内容(假设 Python 2.7)。
有关更详细的详细信息,请参阅文档:
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...
于 2012-06-15T04:51:56.277 回答