I'm new to flask and python and am working on a project where we have a web site that can be reached via two different domains. The code base is the same, but the domains are branded differently. I'll need to load a domain-specific stylesheet and in a couple of template blocks I'll need to use some conditionals to only display some content if I'm on a certain domain.
I'd think the best way to do this is to create a session variable based on the domain (other suggestions welcome). It gets set when a visitor comes in on one of the domains, and then I can use conditionals to load the appropriate stylesheet/block of code.
I'm having trouble getting this working though. Right now in my app.py file, I have a "client" session variable that is set based on url parameters that is being called before each request:
@app.before_request
def set_client_session():
if 'client' in request.args:
session['client'] = request.args['client']
How can I set it using the domain instead of URL params, and how can I check its value in a template so that I can conditionally load a stylesheet/block of code?
Full app.py file:
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)