1、下载必要插件

pip install flask-restful

  flask-restfulDjangorestful模块类似,但是操作要简单许多

2、加载模块

main.py

 3、使用视图类定义API

views.py

form main import api
from
flask import jsonify from models import * from flask_restful import Resource @api.resource("/api/books/") class BookApi(Resource): def __init__(self): super(BookApi, self).__init__() self.result = { "Code": 200, "Version": "1.0", } def set_data(self, books): result_data = { "id": books.id, "title": books.title, "detail_description": books.detail_description, "picture": books.picture, "author": books.author, "publish_time": books.publish_time, "price": books.price, "type": Types.query.get(books.type_id).type } return result_data def get(self): """ 从数据库中获取数据 :return: 返回获取到的数据,200:获取成功,400:获取失败 """ self.result["Method"] = "get" columns = ["start_page", "limit"] data = request.args if data: limit = int(data.get("limit", 10)) start_page = (int(data.get("start_page", 1)) - 1 if (int(data.get("start_page", 1)) > 0) else 0) * limit id_list = Books.query.with_entities(Books.id).all()[start_page:start_page + limit] page_range = [i[0] for i in id_list] books = Books.query.filter(Books.id.in_(page_range)).all() book_list = [] for book in books: book_list.append(self.set_data(book)) self.result["Data"] = book_list else: books = Books.query.with_entities(Books.id).all() total = len(books) self.result["Code"] = 400 self.result["Total"] = total self.result["Columns"] = columns return jsonify(self.result) def post(self): """ 这里是post请求,负责保存数据 :return: 返回数据保存的状态,200:保存成功,400:保存失败 """ self.result["Method"] = "post" columns = sorted(["title", "detail_description", "picture", "author", "publish_time", "price", "type"]) data = request.form columns_post = [i for i in data] columns_post.sort() if data: if columns != columns_post: self.result["Code"] = 400
          self.result["Columns"] = columns else:
          self.result["Code"] = 200 session
= models.session() session.add(Books( author=data.get("author"), detail_description=data.get("detail_description"), picture=data.get("picture"), price=data.get("price"), publish_time=data.get("publish_time"), title=data.get("title"), type_id=Types.query.filter_by(type=data.get("type")).first().id, )) session.commit() else: self.result["Code"] = 400 self.result["Columns"] = columns return jsonify(self.result) def put(self): """ 负责修改数据 :return:返回修改的状态,200:修改成功,400:修改失败 """ self.result["Method"] = "put" data = request.form columns = sorted(["id", "title", "detail_description", "picture", "author", "publish_time", "price", "type"]) columns_put = [i for i in data] if "id" in columns_put and set(columns) >= set(columns_put): self.result["Code"] = 200 id = int(data.get("id")) book = Books.query.get(id) for key, value in data.items(): if key not in ["id", "type"]: setattr(book, key, value) book.type_id = int(data.get("type", book.type_id)) book.save() else: self.result["Code"] = 400 self.result["Columns"] = columns return jsonify(self.result) def delete(self): """ 负责删除数据 :return: 返回删除的状态,200:删除成功,400:删除失败 """ data = request.form self.result["Method"] = "delete" if data: self.result["Code"] = 200 id = int(data.get("id")) book = Books.query.get(id) book.delete() else: self.result["Code"] = 400 self.result["Columns"] = ["id"] return jsonify(self.result)

  这里为一个数据表建立API,数据表含有的字段如下:

   API视图类中包含4个基本方法,分别是get、post、put、delete,分别代表网络访问时对应的4个请求和数据库的增删改查操作,get方法用于查询数据,post方法用于增加数据,put方法用于修改数据,delete用于删除数据

4、API的使用

(1)get请求

   发送get请求,将会访问视图类的get方法,返回对应信息,这里我的get请求没有设置key=value,按照代码上所写的返回能指定的key和数据的总条数

(2)post请求

   发送post请求将会执行post方法,将数据保存到数据库,查看刚保存下来的数据

 (3)put请求

   发送put请求则执行put方法,用于修改数据,可指定要修改的字段,将会根据id将对应字段的数据进行修改,查看修改过后的数据:

 (4)delete请求

   发送delete请求将会执行delete方法,通过指定id来删除数据库中对应数据,查看被删除的数据是否还存在:

12-29 16:56