python篇-常用库08-Flask框架(图文详解)

一、 简介

Flask是一个非常小的PythonWeb框架,被称为微型框架;只提供了一个稳健的核心,其他功能全部是通过扩展实现的。

二、 概要

安装: pip install flask
使用到装饰器:以@开头的代码方法

三、 实例如下

from flask import Flask, request, jsonify

# 用当前脚本名称实例化Flask对象,方便flask从该脚本文件中获取需要的内容
app = Flask(__name__)

#在Flask中,路由是通过@app.route装饰器(以@开头)来表示
@app.route("/")
def index():
    return "Hello World!"

#methods参数用于指定允许的请求格式,#常规输入url的访问就是get方法
@app.route("/hello",methods=['GET','POST'])
def hello():
    return "Hello World!"

#注意路由路径不要重名,映射的视图函数也不要重名
@app.route("/hi",methods=['POST'])
def hi():
    return "Hi World!"


#指定参数
    #string 接受任何不包含斜杠的文本
    #int 接受正整数
    #float 接受正浮点数
    #path 接受包含斜杠的文本

@app.route("/index/<int:id>",)
def index_args(id):
    if id == 1:
        return 'first'
    elif id == 2:
        return 'second'
    elif id == 3:
        return 'thrid'
    else:
        return 'hello world!'


#request对象的使用
@app.route("/index/request",)
def index_request():
    if request.method == 'GET':
        return render_template('index.html')
    el
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值