2014-03-12 29 views
1

我有一个网站需要重新命名,具体取决于访问者的网址。大多数情况下,内容是相同的,但CSS不同。我是全新的瓶子,对会话cookie比较新,但我认为最好的办法是创建一个包含“客户端”会话变量的会话cookie。然后,根据客户端(品牌),我可以将特定的css包装附加到模板。烧瓶:从URL参数设置会话变量

如何访问URL参数并将其中一个参数值设置为会话变量?例如,如果访问者在www.example.com/index?client=brand1上,我想设置会话['client'] = brand1。

我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.route('/') 
def index(): 
    session['client'] = 
    return render_template('index.html') 

@app.route('/edc') 
def edc(): 
    return render_template('pages/edc.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 install(): 
    return render_template('pages/install.html') 

@app.route('/uninstall') 
def uninstall(): 
    return render_template('pages/uninstall.html') 

if __name__ == '__main__': 
    app.run(debug=True) 

回答

4

你可以在@flask.before_request装饰功能,这样做的:

@app.before_request 
def set_client_session(): 
    if 'client' in request.args: 
     session['client'] = request.args['client'] 

set_client_session将在每个传入的请求被调用。