这里是你的通告内容

春生

Be humble, communicate clearly, and respect others.

Flask - 架构设计

文件结构

项目配置

config.py


import os
import pymysql

class Config(object):

    ECRET_KEY = os.environ.get('SECRET_KEY') or 'secret key'
    # app.secret_key='sjehfjeefrjewth43u'  #设置session加密
    JSON_AS_ASCII=False  #指定json编码格式 如果为False 就不使用ascii编码,
    JSONIFY_MIMETYPE="application/json;charset=utf-8" #指定浏览器渲染的文件类型,和解码格式;
    SESSION_PERMANENT = False
    SESSION_TYPE = "filesystem"
    # 数据库链接的配置,此项必须,格式为(数据库+驱动://用户名:密码@数据库主机地址:端口/数据库名称)
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:root123456@49.233.175.110:3306/db'
    SQLALCHEMY_TRACK_MODIFICATIONS = True  # 跟踪对象的修改,在本例中用不到调高运行效率,所以设置为False

ext.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

静态文件static

app应用

models.py

from HandlingProject.ext import db

class Userinfo(db.Model):  # 继承SQLAlchemy.Model对象,一个对象代表了一张表

    id= db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)  # id 整型,主键,自增,唯一
    username = db.Column(db.String(20))  # 名字 字符串长度为20
    password = db.Column(db.String(20))  # 年龄 整型,默认为20

    __tablename__ = 'userinfo'  # 该参数可选,不设置会默认的设置表名,如果设置会覆盖默认的表名

    def __init__(self, username, password):  # 初始化方法,可以对对象进行创建
        self.username = username
        self.password = password

views.py

from flask import Blueprint, request, redirect
from flask import session

from users.models import Userinfo

from flask import render_template

user = Blueprint('users', __name__, template_folder='templates', static_folder='staitc')


@user.before_request
def check(*args, **kwargs):
    # 中间间 登陆认证
    if request.path == '/login/':
        return None
    elif session.get('user'):
        return None
    else:
        return redirect('/login/')


@user.route("/login/", methods=["GET", "POST"])
def login():
    # 登陆
    code = {"info": None, }
    username = request.args.get("username")
    password = request.args.get("password")
    obj = Userinfo.query.filter(Userinfo.username == username, Userinfo.password == password).first()

    if obj:
        session['user'] = obj.username
        code["info"] = "登陆成功"
        return redirect('/index/')
    else:
        code["info"] = "密码或者账号不正确"
    return render_template("login.html", code=code)


@user.route('/index/')
def index():
    # 首页
    return render_template('index.html')


@user.route("/logut/")
def logout():
    # 注销
    del session["user"]
    return redirect('/login/')

启动文件入口

from flask import Flask
from users.views import user
from youtube.views import youtu_video
from flask_session import Session
from youtube.models import Author,Video

from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from HandlingProject.config import Config
from HandlingProject.ext import db

app = Flask(__name__)

manager = Manager(app=app)  # 初始化manager模块

app.config.from_object(Config)
db.init_app(app)

with app.app_context():
    db.create_all()


Session(app)


app.register_blueprint(user, url_prefix=None)
app.register_blueprint(youtu_video, url_prefix='/youtu_video/')


app.run(host='127.0.0.1', port=9090, debug=True)

posted @ 2020-06-17 16:59  PythonAV  阅读(666)  评论(0编辑  收藏  举报