python - 使用中间件类通过应用程序引擎进行移动检测的简单场景

标签 python google-app-engine jinja2

我是应用程序引擎和 python 的新手,我只是想了解事物如何工作的基本概念。

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

这是我的 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]
    """

最佳答案

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]
    """

关于python - 使用中间件类通过应用程序引擎进行移动检测的简单场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14338217/

相关文章:

python - Yosemite 上的常见 Python 错误

python - Popen 给出 "File not found"错误 (windows/python)

python - SQLAlchemy 关系过滤器?

python - 谷歌应用引擎日志记录系统中的无限递归

python - 如何使用多组模板为 Flask 应用程序组织代码

python - 如何增加 flask 形式的 bool 字段的大小?

python - 不支持 pycharm flask 项目

python - 数据读取-csv

google-app-engine - 在任务队列中添加任务

java - 部署我的应用程序时,临时目录中缺少 Google appengine sdk jar 文件