gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:07:23 25 4
gpt4 key购买 nike

我是应用程序引擎和 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com