2014-03-12 2 views
1

방문자가 들어오는 URL에 따라 브랜드 이름을 변경해야하는 웹 사이트가 있습니다. 대부분 내용은 동일하지만 CSS가 다릅니다. 나는 술에 아주 새롭고 세션 쿠키에 상대적으로 새로운 것이지만, 이것을하는 가장 좋은 방법은 "클라이언트"세션 변수를 포함하는 세션 쿠키를 만드는 것입니다. 그런 다음 클라이언트 (브랜드)에 따라 특정 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 각 들어오는 요청에 호출됩니다.

관련 문제