zoukankan      html  css  js  c++  java
  • flask项目结构(五)使用数据库

    简介:

    基础搭建好了,开始读写数据库吧。毕竟写的程序,也没什么高深的,就是CRUD,中文说是增删改查。

    一:在数据库中增加测试数据。

    在项目根目录建立init_test.py

    from config import Base,db_session
    from models.models import *
    from app.app01.app01_models import *
    
    add_public=public(
        name='jack_public',
        email='jack_public@jackadam.ml',
    )
    add_private=private(
        name='jack_private',
        email='jack_private@jackadam.ml',
    )
    db_session.add(add_public)
    db_session.add(add_private)
    db_session.commit()
    db_session.remove()
    public_user=public.query.filter_by(public_name='jack_public').first()
    print(public_user.public_email)
    private_user=private.query.filter_by(private_name='jack_private').first()
    print(private_user.private_email)

    执行结果为:
    jack_public@jackadam.ml
    jack_private@jackadam.ml
    读取两个邮件地址。

    二:在app/main中使用

    修改/app/main/views.py

    from flask import Blueprint
    from models.models import *  # 新加,引入models
    from app.app01.app01_models import *  # 新加,引入models
    
    main = Blueprint('main', __name__)
    
    
    @main.route('/')
    def show():
        public_user = public.query.filter_by(public_name='jack_public').first()  # 新增数据库查询
        private_user = private.query.filter_by(private_name='jack_private').first()  # 新增数据库查询
        msg = public_user.public_email + '<p>' + private_user.private_email  # 组织返回代码
        return msg  # 修改返回内容为刚组织好的代码

    三:本机测试

    我们只修改了默认首页的返回。首页返回数据如下

    四:同步到服务器

    syncthing同步

    五:生成新docker镜像

    ./rebuild.sh

    六:测试服务器运行状态

    我的服务器及发布端口是8800

    七:写个测试脚本

     上次就是部署之后老断开数据库连接。

    那么这次写个脚本进行循环测试:

    import requests
    r=requests.get('http://192.168.1.3:8800')
    print(r.status_code)
    import time
    t2 = time.strftime('%Y-%m-%d %H:%M:%S')
    print(t2)
    i = 1
    while True:
        try:
            r=requests.get('http://192.168.1.3:8800/app02',timeout=2)
            if r.status_code !=200:
                print(time.strftime('%Y-%m-%d %H:%M:%S')+'-----------bed')
                break
            else:
                print(time.strftime('%Y-%m-%d %H:%M:%S')+'--ok')
            time.sleep(1)
            i+=1
        except:
            print(time.strftime('%Y-%m-%d %H:%M:%S') + '-----------bed')

    代码写的烂,也没存记录。

    就是一直傻跑而已。

    如果错了,那就停了

    十:遗漏

    现在还差路由,静态文件,权限几部分。

  • 相关阅读:
    LeetCode-Letter Combinations of a Phone Number
    LeetCode-Sort Colors
    C++Memset误区
    LeetCode-Valid Palindrome
    LeetCode-Longest Consecutive Sequence
    C++豆知识索引
    C++ 哈希表
    LeetCode-Sum Root to Leaf Numbers
    LeetCode-Word LadderII
    LeetCode-Word Ladder
  • 原文地址:https://www.cnblogs.com/jackadam/p/8684905.html
Copyright © 2011-2022 走看看