我正在努力讓Jinja2能夠與Google AppEngine一起工作。我有我的main.py代碼如下:需要幫助才能讓jinja2工作
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文件位於同一目錄中。
我看不錯。你有什麼錯誤嗎?您是否嘗試過部署到GAE?如果是這樣,它看起來如何?你有'app.yaml'中指定的jinja2嗎? 'jinja_environment'行只是告訴jinja在哪裏可以找到相對於當前文件路徑的templates目錄。所以如果你在'main.py'文件中有這樣的代碼,你需要'''目錄與main.py'文件在同一層。 – bernie
這是一個非常簡單 - 但確認工作! - 這個應用程序可以幫助你設想事情是如何結合在一起的:https://bitbucket.org/abernier/anotherday/src但是你的代碼真的很好。 – bernie
當我部署到GAE時,它只是給我一個500服務器錯誤。並且我沒有收到任何其他錯誤 – clifgray