Flask:从URL Param设置会话变量

2024-05-19 15:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网站,需要重新命名取决于网址,一个访问者来了。在大多数情况下,内容是相同的,但是CSS是不同的。我对flask是全新的,对session cookies也比较陌生,但我认为最好的方法是创建一个包含“client”会话变量的会话cookie。然后,根据客户机(品牌),我可以将特定的css包装器附加到模板中。在

如何访问URL参数并将其中一个param值设置为会话变量?例如,如果有客人进来www.example.com/index?client=brand1,那么我想设置session['client']=brand1。在

我的应用程序副本文件:

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)

Tags: importclientappindexreturnsessiondefhtml
1条回答
网友
1楼 · 发布于 2024-05-19 15:20:19

您可以在^{}修饰函数中执行此操作:

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

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

相关问题 更多 >