2
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()

工作就好。但是,如果我将第二条路由更改为@app.route('/<path:page>'),那么对 URL 的任何访问都会/path/to/page产生 404。

为什么不起作用@app.route('/<path:page>')

相关问题,但不回答问题:

4

2 回答 2

3

static_url_path与路由冲突。Flask 认为之后的路径/是为静态文件保留的,因此path转换器不起作用。请参阅:Flask 开发服务器中静态文件的 URL 路由冲突

于 2013-10-12T07:49:05.647 回答
1

对我来说完美无瑕:

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()

我可以访问:http://localhost/->indexhttp://localhost/page/<any index/path e.g.: 1>->article

于 2013-10-11T23:46:23.897 回答