[英]GAE app main.py request handlers
我一直在關注GAE / Jinja2教程,並且值得慶幸的是,它結合了我一直在GAE中苦苦掙扎的功能,該功能是如何使用main.py文件鏈接html頁面,以便可以使用Jinja2對其進行編輯。 main.py的代碼如下。
import webapp2
import logging
import jinja2
import os
jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(os.path.dirname(__file__) + "/templates"))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {
'welcome':'Welcome to my website!',
}
template = jinja_environment.get_template('homepage.html')
self.response.write(template.render(template_values))
class FeedbackPage(webapp2.RequestHandler):
def get(self):
feedbackvalues = {
}
template = jinja_environment.get_template('feedbackform.html')
class TopFinishers(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('Top10Finishers.html')
class Belts(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('WWETitlesBelt.html')
class TopWrestlers(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('Top10Wrestlers.html')
app = webapp2.WSGIApplication([('/',MainPage), ('/feedbackform.html',FeedbackPage),
('/Top10Finishers.html',TopFinishers),
('/WWETitlesBelt.html',Belts),
],
debug=True)
在本教程中,我按照添加更多請求處理程序,然后在app對象中實例化它們的過程進行操作。 但是,當我通過單擊頁面上的按鈕加載頁面時,將帶我到空白頁面。 當我單擊以轉到“十大完成者”時,由於URL為“ localhost:etc / Top10Finishers.html”,它將成功將我帶到該頁面。
但是,內容未顯示,我是否需要在app.yaml文件中添加任何URL處理程序?
application: 205semestertwo
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /css
static_dir: styling
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: "2.6"
我的問題是“什么導致此錯誤”? 由於控制台日志似乎沒有給我任何錯誤或見解
您已成功在每個處理程序上檢索了新模板,但忘記了在響應中編寫它,就像對主處理程序所做的一樣:
class TopFinishers(webapp2.RequestHandler):
def get(self):
values = {}
template = jinja_environment.get_template('Top10Finishers.html')
self.response.write(template.render(values))
這適用於所有處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.