1

我是应用程序引擎和 python 的新手,我只是想了解事物的工作原理。

我有一个简单的应用程序,有一个映射的 url (/)。我尝试使用的所有类都在应用程序的基本目录中。

这是我的 main.py - 我要做的就是使用中间件类将变量传递给模板,这样我就可以根据设备类型渲染页面的不同部分。

import webapp2
import jinja2
import os
from useragents import search_strings

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):

    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage)],
                          debug=True)


class Middleware(object):
@staticmethod
def process_request(request):
    """Adds a "mobile" attribute to the request which is True or False
       depending on whether the request should be considered to come from a
       small-screen device such as a phone or a PDA


    //rest of class is [here][1]
    """
4

1 回答 1

1
import webapp2
import jinja2
import os
from useragents import search_strings

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):
    #i don't know if you want to overwrite self.request but here it is
    self.request = Middleware.process_request(self.request)
    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage)],
                          debug=True)


class Middleware(object):
@staticmethod
def process_request(request):
    """Adds a "mobile" attribute to the request which is True or False
       depending on whether the request should be considered to come from a
       small-screen device such as a phone or a PDA


    //rest of class is [here][1]
    """
于 2013-01-15T13:51:46.593 回答