首页 > 其他分享 >flask后端简单demo

flask后端简单demo

时间:2022-10-01 11:45:48浏览次数:81  
标签:__ username 后端 flask demo db json user password

1、flask demo

from flask import Flask, Blueprint, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

# 数据库
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:[email protected]:3306/flask'
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(), unique=True)
    password = db.Column(db.String(), unique=True)

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def __eq__(self, other):
        # `__eq__` is an instance method, which also accepts one other object as an argument.
        if type(other) == type(self) and other.username == self.username and other.password == self.password:
            return True
        else:
            return False

    def __str__(self):
        return "User(%s,%s)" % (self.username, self.password)


# 蓝图
bp_index = Blueprint("index_page", __name__)
bp_sql = Blueprint("sql_page", __name__)


@bp_index.route("/")
def index():
    return "hello world"


@bp_sql.route("/", methods=['GET'])
def list_users():
    from sqlalchemy import text
    sql = text("SELECT * FROM  user ")
    result = db.engine.execute(sql)
    msg = ''
    for row in result:
        msg = msg + str(row) + "\n"
    return msg


@bp_sql.route("/", methods=['POST'])
def create_user():
    user_json = request.get_json()
    username = user_json.get("username")
    password = user_json.get("password")
    user = User(username, password)
    db.session.add(user)
    db.session.commit()
    return "add success"


@bp_sql.route("/", methods=['PUT'])
def update_user():
    user_json = request.get_json()
    username = user_json.get("username")
    password = user_json.get("password")
    id = user_json.get("id")
    db.session.query(User).filter(User.id == id).update({"username": username, "password": password})
    db.session.commit()
    return "update success"


@bp_sql.route("/", methods=['DELETE'])
def delete_user():
    user_json = request.get_json()
    username = user_json.get("username")
    password = user_json.get("password")
    del_user = User(username, password)
    users = User.query.all()
    for u in users:
        if u == del_user:
            db.session.delete(u)
            db.session.commit()
    return "delete success"


app.register_blueprint(bp_index, url_prefix="/")
app.register_blueprint(bp_sql, url_prefix="/sql")

if __name__ == '__main__':
    app.run()

2、Postman测试

显示用户

image-20221001105618322

添加用户

image-20221001105659819

更新用户

image-20221001105713789

删除用户

image-20221001105817641

标签:__,username,后端,flask,demo,db,json,user,password
From: https://www.cnblogs.com/hery27/p/16746973.html

相关文章

  • NFC卡启动web应用程序 - demo
    废话不多说,先上视频TRANSLATEwithxEnglishArabicHebrewPolishBulgarianHindiPortugueseCatalanHmongDawRomanianChineseSimplifiedH......
  • flask基础知识
    1、flask微服务含义“微”并不代表整个应用只能塞在一个Python文件内,当然塞在单一文件内也没有问题。“微”也不代表Flask功能不强。微框架中的“微”字表示Flask......
  • springboot+vue前后端分离项目搭建
    今天开始学习springboot+vue的前后端分离项目,跟着bili的视频开始做起。第一步安装node.js node.js自带npm插件,npm是node的一个包管理工具,安装好了node.js,就安装了npm......
  • 五、flask—日志
    #-*-coding:utf-8-*-#@Time:2022/10/01#@Author:chron#@FileName:hello_world.py#@Software:PyCharm#@E-mail:[email protected]......
  • 四、flask—session会话
    #-*-coding:utf-8-*-#@Time:2022/10/01#@Author:chron#@FileName:hello_world.py#@Software:PyCharm#@E-mail:[email protected]......
  • 三、flask—获取请求信息
    #-*-coding:utf-8-*-#@Time:2022/10/01#@Author:chron#@FileName:hello_world.py#@Software:PyCharm#@E-mail:[email protected]......
  • 二、flask—http请求方法
    #-*-coding:utf-8-*-#@Time:2022/10/01#@Author:chron#@FileName:hello_world.py#@Software:PyCharm#@E-mail:[email protected]......
  • 一、初识flask
    #-*-coding:utf-8-*-#@Time:2022/10/01#@Author:chron#@FileName:hello_world.py#@Software:PyCharm#@E-mail:[email protected]......
  • Demo10 java基础09-10
    packageoperator;importcom.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;importjava.sql.SQLOutput;publicclassDemo02{publicstaticvoidmain(String[]a......
  • DEMO 双ALV 简单案例
    货铺QQ群号:834508274简单案例,上面ALV是Header,下面是Item。一个屏幕,俩控件效果:上面是BKPF数据,下面是BSEG数据.完整代码:*&-------------------------------------------------......