Flask+Alchemy简单测试

壤驷深
2023-12-01

Flask+Alchemy简单测试:

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
import logging,time,sys,os,platform,logging.handlers
from flask.logging import default_handler





# LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
# formatter = logging.Formatter(LOG_FORMAT)
# handler = logging.StreamHandler(sys.stdout)
# handler.setFormatter(formatter)

# log = logging.getLogger("test")
# log.setLevel(logging.DEBUG)
# log.addHandler(handler)
ttt = logging.handlers.TimedRotatingFileHandler("url.txt", when="m", interval=1, backupCount=10, encoding='UTF-8')
handler = logging.FileHandler('log.txt', encoding='UTF-8')
logging_format = logging.Formatter(
    '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
logging.basicConfig(handlers=[ttt],format='%(asctime)s - %(levelname)s - %(message)s')


host = "0.0.0.0"
port = 8080

app = Flask(__name__)
app.logger.addHandler(handler)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite'
db = SQLAlchemy(app)


class students(db.Model):
    id = db.Column('student_id', db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    city = db.Column(db.String(50))
    addr = db.Column(db.String(200))
    pin = db.Column(db.String(10))

    def __init__(self, name, city, addr, pin):
        self.name = name
        self.city = city
        self.addr = addr
        self.pin = pin


@app.route('/')
def hello_world():
    return '<h1>Hello tt!</h1>'


@app.route('/a')
def test():
    my_str = 'Hello Word'
    my_int = 10
    my_array = [0]
    my_dict = {
        'name': 'xiaoming',
        'age': 18
    }
    return render_template('index.html',
                           my_str=my_str,
                           my_int=my_int,
                           my_array=my_array,
                           my_dict=my_dict
                           )

if __name__ == '__main__':
    app.logger.critical("critical")
    app.logger.warning("测试")
    print(app.logger)
    app.run(host, port, True)

 

 类似资料: