0
我是燒瓶和python的新手,我正在開發一個項目,我們有一個可以通過兩個不同的域名訪問的網站。代碼庫是相同的,但是域名的標籤是不同的。我需要加載一個特定於領域的樣式表,並且在幾個模板塊中,我需要使用一些條件來顯示某些內容(如果我在某個域中)。燒瓶:根據域名設置會話cookie
我認爲最好的方法是創建一個基於域的會話變量(其他建議歡迎)。當訪問者進入其中一個域時,它會被設置,然後我可以使用條件來加載相應的樣式表/代碼塊。
雖然我無法正常工作。現在在我的app.py文件,我有一個「客戶」會議是事先設定的網址參數被每個請求之前稱爲變量:
@app.before_request
def set_client_session():
if 'client' in request.args:
session['client'] = request.args['client']
我怎樣才能將它使用的域名,而非網址params,以及如何在模板中檢查其值,以便可以有條件地加載樣式表/代碼塊?
完全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)
該域設置正確,但條件不是。我正在本地開發服務器上工作,所以現在域名是localhost:5000。當我檢查session.domain =='localhost:5000'的值時,它返回false。如果我只檢查是否有session.domain,它將返回true。當我輸出{{session.domain}}時,肯定是localhost:5000,所以出於某種原因檢查session.domain =='localhost:5000'是否無效。 – dagarre
非常奇怪。我只是自己測試一下,確保它完美運行。 '{{session.domain =='localhost:5000'}}'相等檢查在我的結尾處返回True。這很愚蠢,但試着做'{{'a'=='a'}}'並且看它是否返回True – vivekagr