Forked from topherPedersen/mobile_friendly_flask_app.py
Created
August 26, 2024 21:40
-
-
Save pije76/70fcfdc7e329deb2d04e4a8ae48bf5c4 to your computer and use it in GitHub Desktop.
How to Serve Up Mobile and Desktop Versions of Your Website in Python with Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, Response, render_template, request | |
app = Flask(__name__) | |
@app.route('/', methods=['GET', 'POST']) | |
def index(): | |
user_agent = request.headers.get('User-Agent') | |
user_agent = user_agent.lower() | |
# In your templates directory, create a mobile version of your site (mobile.index.html). | |
# Likewise, add your desired desktop template as well (desktop.index.html). | |
if "iphone" in user_agent: | |
return render_template('mobile.index.html') | |
elif "android" in user_agent: | |
return render_template('mobile.index.html') | |
else: | |
return render_template('desktop.index.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment