Search code examples
python-3.xflaskurl-parametersurlparse

Pass file path as parameter in Flask app.route()


Here is my code. I'm trying to take the file path in as a parameter but the "/" in the path between directories is making the method treat the directories as separate parameters.

from flask import Flask, abort, redirect, url_for, send_from_directory

app = Flask(__name__)


@app.route('/<a>/<b>/<c>/<d>/<e>/<f>')
def url(a, e, b, c, d, f):
    if f == '':
        f = 'index.html'
    print(f)


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True, port=80)

Basically the variable f is supposed to take the file path (a,b,c,d,e are just other parameters) and the method is treating f as multiple parameters when there's a / inputted. Are there any fixes without changing the splitting variable?


Solution

  • Try this:

    @app.route('/<a>/<b>/<c>/<d>/<e>/<path:f>')
    

    See: http://flask.pocoo.org/snippets/76/