我是app引擎和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]
"""
答案 0 :(得分: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]
"""