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/蓝月




原文地址:https://www.cnblogs.com/viplanyue/p/12700694.html