2014-03-12 28 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將在每個傳入的請求被調用。