Add a prefix to all Flask routes

You can put your routes in a blueprint: bp = Blueprint(‘burritos’, __name__, template_folder=”templates”) @bp.route(“https://stackoverflow.com/”) def index_page(): return “This is a website about burritos” @bp.route(“/about”) def about_page(): return “This is a website about burritos” Then you register the blueprint with the application using a prefix: app = Flask(__name__) app.register_blueprint(bp, url_prefix=’/abc/123′)

Get a variable from the URL in a Flask route

This is answered in the quickstart of the docs. You want a variable URL, which you create by adding <name> placeholders in the URL and accepting corresponding name arguments in the view function. @app.route(‘/landingpage<id>’) # /landingpageA def landing_page(id): … More typically the parts of a URL are separated with /. @app.route(‘/landingpage/<id>’) # /landingpage/A def landing_page(id): … Read more

How to debug a Flask app

Running the app in development mode will show an interactive traceback and console in the browser when there is an error. To run in development mode, set the FLASK_ENV=development environment variable then use the flask run command (remember to point FLASK_APP to your app as well). For Linux, Mac, Linux Subsystem for Windows, Git Bash … Read more

Reference template variable within Jinja expression

Everything inside the {{ … }} is a Python-like expression. You don’t need to use another {{ … }} inside that to reference variables. Drop the extra brackets: <h1>you uploaded {{ name }}<h1> <a href=”https://stackoverflow.com/questions/32024551/{{ url_for(“moremagic’, filename=name) }}”>Click to see magic happen</a> (Note that the url_for() function takes the endpoint name, not a URL path; … Read more

How to pass a variable between Flask pages?

If you want to pass some python value around that the user doesn’t need to see or have control over, you can use the session: @app.route(‘/a’) def a(): session[‘my_var’] = ‘my_value’ return redirect(url_for(‘b’)) @app.route(‘/b’) def b(): my_var = session.get(‘my_var’, None) return my_var The session behaves like a dict and serializes to JSON. So you can … Read more