简体   繁体   中英

GAE app main.py request handlers

I have been following a GAE/Jinja2 tutorial, and thankfully it incorporates a feature I have been struggling with within GAE, which is how to link the html pages using the main.py file so they can be edited using Jinja2. The code for main.py is below.

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)

Within the tutorial I followed the procedure for adding more request handlers and then instantiating them in the app object. However, when I load the page by clicking on the button on the page it takes me to a blank page. When I click to go to 'Top 10 Finishers' it will successfully take me to the page as the URL is 'localhost:etc/Top10Finishers.html.

However, the content is not showing, do I need to add any URL handlers within the app.yaml file?

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"

My question is 'What is causing this error'? As the console logs do not appear to be giving me any error or insight

You are successfully retrieving the new template on each handler, but forgot to write it on the response, the same way you did for your main handler:

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

This applies to all your handlers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM