python Flask引擎中的动态子域
a.example.com b.example.com 我想在烧瓶中放入相同的应用程序文件夹,不同的配置文件。 我找到了以下解决方案,但如何使用它们?
创建一个上下文处理器,将“request.host”注入到您的 模板并相应地分支。
为了获得更多控制,您可以创建一个 Site 对象,从 当前请求,并向其添加属性,例如:
class Site(object):
def __init__(self, request):
self.host = request.host
@cached_property
def google_analytics_id(self, default=''):
if self.host == 'python.example.com':
return <something>
elif self.host == 'apple.example.com':
return <something else>
return default
然后在上下文处理器中使用 site = Site(request) 并引用 地点。在你的模板中。其他房产的候选者 可能是 HTML 元描述和关键字、网站标题等。 这种分支只能从应用程序的某些部分进行 当然,可以访问请求对象。
保罗
a.example.com
b.example.com
I want to put in in flask with the same application folder,diferrent config files.
I found the following solution,but how to use them?
Create a context processor that injects 'request.host' into your
templates and branch accordingly.
For more control, you could create a Site object, instantiated from
the current request, and add properties to that, for example:
class Site(object):
def __init__(self, request):
self.host = request.host
@cached_property
def google_analytics_id(self, default=''):
if self.host == 'python.example.com':
return <something>
elif self.host == 'apple.example.com':
return <something else>
return default
Then use site = Site(request) in your context processor and refer to
site. in your templates. Candidates for other properties
might be HTML meta description and keywords, the site's title etc.
This kind of branching is only possible from parts of the application
that have access to the request object, of course.
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 Flask 文档,通过应用程序工厂和应用程序调度实现域处理功能非常容易,这在“Flask 模式”部分中有清楚的解释:
http://flask.pocoo.org/docs/patterns/appdispatch/
http://flask.pocoo.org/docs/patterns/appfactories/
Take a look at flask Document, It's quite easy to implement a domain handling function via Application Factories and Application Dispatching which is clearly explained in 'Patterns for Flask' section:
http://flask.pocoo.org/docs/patterns/appdispatch/
http://flask.pocoo.org/docs/patterns/appfactories/