黑马程序员技术交流社区

标题: 【上海校区】Flask 入门(二) [打印本页]

作者: 梦缠绕的时候    时间: 2018-7-17 09:54
标题: 【上海校区】Flask 入门(二)
本文主要讲的是路由(动态url)

修改index.py中的代码:


from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
        return '<h1>welcome to the system</h1>'

@app.route('/menu')
def menu():
return 'this is the menu of the system'

@app.route('/quit')
def quit():
        return 'you will exit the system'
@app.route('/user/<name>')
def info(name):
    return 'hello,%s' % name

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

路由说白了就是导航,根据uri的文件名部分(uri详解请看https://blog.csdn.net/no_loafer/article/details/71486654)访问(执行)不同的函数.
上文代码执行效果如下:
(1)若访问127.0.0.1:5000

(2)若访问127.0.0.1:5000/menu

(3)若访问127.0.0.1:5000/quit

(4)若访问127.0.0.1:5000/user/蓝月





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2