【问题标题】:GAE app main.py request handlersGAE 应用程序 main.py 请求处理程序
【发布时间】:2015-04-10 00:24:24
【问题描述】:

我一直在关注 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)

在本教程中,我遵循了添加更多请求处理程序然后在应用程序对象中实例化它们的过程。但是,当我通过单击页面上的按钮加载页面时,它会将我带到一个空白页面。 当我点击进入“Top 10 Finishers”时,它会成功将我带到该页面,因为 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"

我的问题是“导致此错误的原因是什么”?由于控制台日志似乎没有给我任何错误或见解

【问题讨论】:

    标签: python google-app-engine jinja2


    【解决方案1】:

    您已在每个处理程序上成功检索新模板,但忘记将其写入响应,就像您为主处理程序所做的那样:

    class TopFinishers(webapp2.RequestHandler):
        def get(self):
            values = {}
            template = jinja_environment.get_template('Top10Finishers.html')
            self.response.write(template.render(values))
    

    这适用于您的所有处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多