3
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:

Community
  • 1
  • 1
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166

2 Answers2

4

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

Community
  • 1
  • 1
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
1

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

Xiao
  • 12,235
  • 2
  • 29
  • 36
m3o
  • 3,881
  • 3
  • 35
  • 56