• flask-restful


    parser = reqparse.RequestParser()
    这种方式采用查询字符串的方式获取前台数据
    request.get_json()
    这种方式采用获取json串的方式获取数据
    flask-restful架构的编码结构:
    flask:
            api:
                __init__.py
                module.py
                view.py
            main.py
    1、main.py
     1 from flask import Flask
     2 from flask_restful import Api
     3 from api.users.users_views import UserApi,UsersApi
     4 
     5 app = Flask(__name__)
     6 api = Api(app)
     7 
     8 api.add_resource(UsersApi, '/users/')
     9 api.add_resource(UserApi, '/users/<name>/')
    10 
    11 @app.route('/')
    12 def hello_world():
    13     return 'Hello World!'
    14 
    15 if __name__ == '__main__':
    16     app.run()
    17 #host='0.0.0.0',port=8080

    2.module.py

     1 list1 = [
     2     {'name':'test', 'mail':'123@123.com', 'passwd':'123456', 'phone':'110110110'}
     3 ]
     4 #get users list
     5 def get_users():
     6     return list1
     7 #add a user
     8 def add_users(info):
     9     username = info['name']
    10     a = test(username)
    11     if a == 1:
    12         return 'already have!'
    13     else:
    14         list1.append(info)
    15         return 'add!'
    16 #get a user
    17 def get_user(username):
    18     for i in range(len(list1)):
    19         if username in list1[i].values():
    20             return list1[i]
    21 #delete a user
    22 def delete_user(username):
    23     for i in range(len(list1)):
    24         if username in list1[i].values():
    25             del(list1[i])
    26             return 'done!'
    27 #exist test
    28 def test(username):
    29     for i in range(len(list1)):
    30         if username in list1[i].values():
    31             t = 1
    32             return t
    33 #change a user
    34 def change_user(info):
    35     username = info['name']
    36     usermail = info['mail']
    37     userpasswd = info['passwd']
    38     userphone = info['phone']
    39     b = test(username)
    40     if b != 1:
    41         return 'no one'
    42     else:
    43         delete_user(username)
    44         dict1 = {}
    45         dict1['name'] = username
    46         dict1['mail'] = usermail
    47         dict1['passwd'] = userpasswd
    48         dict1['phone'] = userphone
    49         list1.append(dict1)
    50         return 'changed!'

    3.view.py

     1 from flask_restful import reqparse, Resource
     2 from flask import request,jsonify
     3 import api.users.users_modules as modules
     4 
     5 parser = reqparse.RequestParser()
     6 parser.add_argument('name', type=str, required=True, help="Can't be empty!")
     7 parser.add_argument('mail')
     8 parser.add_argument('passwd')
     9 parser.add_argument('phone')
    10 
    11 class UsersApi(Resource):
    12     def get(self):
    13             result = modules.get_users()
    14             return jsonify(result)
    15     def post(self):
    16         data = parser.parse_args()
    17         result = modules.add_users(data)
    18         return jsonify(result)
    19     def put(self):
    20         data = parser.parse_args()
    21         result = modules.change_user(data)
    22         return jsonify(result)
    23 
    24 class UserApi(Resource):
    25     def get(self,name):
    26         result = modules.get_user(name)
    27         return jsonify(result)
    28     def delete(self,name):
    29         result = modules.delete_user(name)
    30         return jsonify(result)
    博文欢迎转载,转载请注明出处
  • 相关阅读:
    HDU 1698-Just a Hook
    HDU 1394 Minimum Inversion Number(线段树)
    HDU 4253-Two Famous Companies(二分+最小生成树)
    POJ 3279
    POJ 2251 Dungeon Master
    POJ1321 棋盘问题
    Charlie's Change POJ
    Coins —— POJ-1742
    sublime text主要快捷键列表
    根据电脑分辨率调整网站的布局
  • 原文地址:https://www.cnblogs.com/yinkemeng/p/13626686.html
Copyright © 2020-2023  润新知