【问题标题】:http://127.0.0.1:5000/post/0 isnt workinghttp://127.0.0.1:5000/post/0 不工作
【发布时间】:2018-06-28 17:29:43
【问题描述】:
from flask import Flask   

app = Flask(__name__)
posts = {0: dict(title='Hello World', content='This is my first ever post !'}

@app.route('/')
def home():
    return 'Hello World!'

@app.route('/post/(int:post_id)') 
def post(post_id):
    post = posts.get(post_id)
    return f"Post {post['title']} , content:\n\n{post['content']}"

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

【问题讨论】:

  • 对于烧瓶内的动态路由,您必须使用<> 而不是()

标签: python flask


【解决方案1】:

使用@app.route('/post/<int:post_id>')。注意<>

【讨论】: