如何在烧瓶中使用url中的变量
问题描述:
我正在按照名为'thenewboston'的家伙在YouTube上提供一个教程。 https://www.youtube.com/watch?v=27Fjrlx4s-o&index=2&list=PL6gx4Cwl9DGDi9F_slcQK7knjtO8TUvUs 下面是我使用的代码。如何在烧瓶中使用url中的变量
from flask import Flask
app = Flask(__name__)
# @ signifies a decorator - way to wrap a function and modifying its behavior
@app.route('/')
def index():
return "this is the homepage"
@app.route('/tuna')
def tuna():
return '<h2> tuna is good</h2>'
@app.route('/profile/<username>')
def profile():
return "hey there %s" % username
if __name__ == "__main__":
app.run()
当我去 http://127.0.0.1:5000/profile/bucky 我得到以下错误
内部服务器错误
服务器遇到一个内部错误,无法完成您的请求。服务器过载或应用程序出现错误。
答
您需要定义函数定义的username
论据profile()
:
@app.route('/profile/<username>')
def profile(username):
return "hey there %s" % username