【发布时间】:2014-04-17 07:48:40
【问题描述】:
我是 Flask 和 python 的新手,我正在做一个项目,我们有一个可以通过两个不同域访问的网站。代码库相同,但域的品牌不同。我需要加载一个特定于域的样式表,并且在几个模板块中我需要使用一些条件来仅在我位于某个域时显示一些内容。
我认为最好的方法是根据域创建一个会话变量(欢迎提出其他建议)。当访问者进入其中一个域时,它会被设置,然后我可以使用条件来加载适当的样式表/代码块。
不过,我无法让它正常工作。现在在我的 app.py 文件中,我有一个“客户端”会话变量,该变量是根据在每个请求之前调用的 url 参数设置的:
@app.before_request
def set_client_session():
if 'client' in request.args:
session['client'] = request.args['client']
如何使用域而不是 URL 参数来设置它,以及如何在模板中检查它的值,以便有条件地加载样式表/代码块?
完整的 app.py 文件:
import os
import json
from flask import Flask, session, request, render_template
app = Flask(__name__)
# Generate a secret random key for the session
app.secret_key = os.urandom(24)
@app.before_request
def set_client_session():
if 'client' in request.args:
session['client'] = request.args['client']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/edc')
def abc():
return render_template('pages/abc.html')
@app.route('/success')
def success():
return render_template('success.html')
@app.route('/contact')
def contact():
return render_template('pages/contact.html')
@app.route('/privacy')
def privacy():
return render_template('pages/privacy.html')
@app.route('/license')
def license():
return render_template('pages/license.html')
@app.route('/install')
def dcm_download():
return render_template('pages/install.html')
@app.route('/uninstall')
def uninstall():
return render_template('pages/uninstall.html')
if __name__ == '__main__':
app.run(debug=True)
【问题讨论】:
标签: python flask session-variables session-cookies