Domanda

from flask import Flask, render_template

app = Flask(__name__, static_url_path='')

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/page/<path:page>')
def article(page):
    return render_template('page.html')

if __name__ == "__main__":
    app.run()

Work just fine. But if i change the second route to @app.route('/<path:page>'), then any access to URL like /path/to/page yields 404.

Why doesn't @app.route('/<path:page>') work?

Related questions, that don't answer the question however:

È stato utile?

Soluzione

static_url_path conflicts with routing. Flask thinks that path after / is reserved for static files, therefore path converter didn't work. See: URL routing conflicts for static files in Flask dev server

Altri suggerimenti

works flawless for me:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/page/<path:page>')
def article(page):
    return render_template('page.html')

if __name__ == "__main__":
    app.debug = True
    app.run()

I can access: http://localhost/ -> index and http://localhost/page/<any index/path e.g.: 1> -> article

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top