Flask learning (1)-basic configuration

app.py

# 1.导入Flask扩展
from flask import Flask
# 2.创建Flask应用程序实例
# 需要传入__name__,作用是为了确定资源所在的路径
app = Flask(__name__)

# 3.定义路由及视图函数
# Flask中定义路由是通过装饰器实现的
# 路由默认只支持GET,如果需要增加,需要自行指定
@app.route('/', methods=['GET', 'POST'])
def hello_world():
    return 'Hello flask aa!'

# 使用同一个视图函数, 来显示不同用户的订单信息
# <>定义路由的参数,<>内需要起个名字
@app.route('/orders/<order_id>')
def get_order_id(order_id):
    # 需要在视图函数的()内填入参数名,那么后面的代码才能去使用
    return 'order_id %s' % order_id


# 4.启动程序
if __name__ == '__main__':
    # 执行了app.run,就会将Flask程序运行在一个简易的服务器(Flask提供的,用于测试的)
    app.run()

Note: If you write and run flask under linux, you cannot access the public network ip through the browser. Many novices may encounter this problem when they start to write. The solution is to add your private ip and port to app.run(), where: the port is open in your security policy group.

app.run(host='192.168.238.129', port=5000, debug=True)

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/111875491