info

本文介绍了Python项目的常见文件结构,包括manage.py的作用、config.py的配置功能,以及info模块和index模块下的初始化文件和视图文件。同时提到了前端主要的main.js文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

manage.py

# 用命令运行
from flask_script import Manager
# 数据库迁移
from flask_migrate import Migrate, MigrateCommand
from info import create_app, db, models

# manage.py是程序启动的入口,只关心启动的相关参数以及内容,不关心具体的
# 创建app或者业务逻辑

# 通过指定的配置名字创建对应配置的app
# create_app就类似于工厂方法
from info.models import User

app = create_app('development')

# 导入Manage 设置命令运行 python manage.py
manager = Manager(app)
# 执行数据库迁移命令 将app与db关联
Migrate(app, db)
# 将迁移命令添加到manager中
manager.add_command('db', MigrateCommand)


# python manage.py createsuperuser -n admin -p 12345678

@manager.option("-n", "-name", dest="name")
@manager.option("-p", "-password", dest="password")
def createsuperuser(name, password):
    if not all([name, password]):
        print("参数不足")

    user = User()
    user.nick_name = name
    user.mobile = name
    user.password = password
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        print(e)
    print("添加成功")


if __name__ == '__main__':
    # app.debug = True
    manager.run()

config.py

# StrictRedis报错,从manage中导入StrictRedis
from redis import StrictRedis
import logging


class Config(object):
    """项目的配置"""

    # 配置调试模式debug
    DEBUG = True

    # 设置秘钥
    SECRET_KEY = "84wZ7e1xEyypZ3NmYym4Z42iCP3TWE2hfUhQ5IaZtXtHyTn19imh70mbOJCNiO98'"

    # 配置mysql数据库
    SQLALCHEMY_DATABASE_URI = "mysql://root:123456@127.0.0.1:3306/information27"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    # 在请求结束时候,如果指定此配置为true 那么SQLAlchemy 会自动执行一次db.sesssion.commit()命令
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

    # 配置redis
    REDIS_PASSWORD = "123456"
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 6379

    # 配置Session
    # session的保存配置
    SESSION_TYPE = "redis"
    # 开启session签名,可将k值加密
    SESSION_USE_SIGNER = True
    # 指定session保存的位置 保存到redis
    SESSION_REDIS = StrictRedis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD)
    # 设置需要过期
    SESSION_PERMANENT = False
    # 设置过期时间 为2天 默认值为31天
    PERMANENT_SESSION_LIFETIME = 86400 * 2

    # 设置日志等级
    LOG_LEVER = logging.DEBUG


class DevelopmentConfig(Config):
    """开发环境下的配置"""
    DEBUG = True


class ProductionConfig(Config):
    """"生产环境下的配置"""
    DEBUG = False
    LOG_LEVER = logging.WARNING


class TestingConfig(Config):
    """单元测试环境下的配置"""
    DEBUG = True
    TESTING = True


config = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "testing": TestingConfig,
}

info/init.py

import logging
from logging.handlers import RotatingFileHandler
from flask import Flask, render_template, g
# 用来指定session存放的位置
from flask_session import Session
# 导入SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
# 导入redis数据库
from flask_wtf.csrf import generate_csrf
from redis import StrictRedis
from config import config

# 初始化数据库
# 在Flask很多扩展里面都可以先初始化扩展的对象,然后去调用init_app方法去初始化

db = SQLAlchemy()

# 定义redis_store全局变量
redis_store = None  # type:StrictRedis
# 用上面和下面的方法都可以使set有提示
# redis_store: StrictRedis = None


def setup_log(config_name):
    # 设置日志的记录等级
    logging.basicConfig(level=config[config_name].LOG_LEVER)  # 调试debug级
    # 创建日志记录器,指明日志保存的路径、maxBytes每个日志文件的最大大小1024 * 1024为1M、backupCount为保存的日志文件个数上限
    file_log_handler = RotatingFileHandler("logs/log", maxBytes=1024 * 1024 * 100, backupCount=10)
    # 创建日志记录的格式 日志等级 输入日志信息的文件名 行数 日志信息
    formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s')
    # 为刚创建的日志记录器设置日志记录格式
    file_log_handler.setFormatter(formatter)
    # 为全局的日
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值