flask web模板二

通过多个蓝图将不同功能的视图函数放到不同的模块文件下

文件目录结构如下
.
├── app
│   ├── __init__.py # 初始化flask核心对象,导入视图函数文件,并注册视图函数到蓝图
│   ├── templates # 模板文件目录
│   │   └── login.html
│   └── views
│   ├── account.py # 创建和账号相关视图函数
│   ├── __init__.py # 定义蓝图,并导入views下的视图函数模块文件
│   ├── user.py # 创建和用户相关的视图函数
│   └── web.py # 创建大部分业务相关视图函数
├── Pipfile
├── run.py # 启动flask,加载配置文件
└── settings.py # 定义配置文件
在项目根目录下创建run.py、settings.py以及app目录,并且在app目录下创建__init__.py以及templates、views、static目录
run.py内容如下

# -*- coding:utf-8 -*-

from app import app
# app.config.from_object("settings.ProductionConfig")
app.config.from_object("settings.DevelopmentConfig")
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

settings.py内容如下

# -*- coding:utf-8 -*-


class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite://:memory:'


class ProductionConfig(Config):
    DATABASE_URI = 'mysql://server:server@192.168.15.120/server'


class DevelopmentConfig(Config):
    DEBUG = True


class TestingConfig(Config):
    TESTING = True

app/__init__.py内容如下

from flask import Flask

app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/static')
from .views.account import account
from .views.user import user
from .views.web import web

app.register_blueprint(account)
app.register_blueprint(user)
app.register_blueprint(web)

app.secret_key = "password"
app.debug = True

在app/views目录下创建__init__.py、account.py、user.py、web.py
__init__.py内容如下

# 定义蓝图

from flask import Blueprint

# 这里定义一个叫web的通用名称的蓝图,在web/__init__.py中将 web目录下的多个视图函数的文件再注册到web这个蓝图下
web = Blueprint('web', __name__)
account = Blueprint('account', __name__)
user = Blueprint('user', __name__, url_prefix='/user/')

from app.views import web
from app.views import user
from app.views import account

web.py内容如下

from app.views import web


@web.route('/', methods=['GET', 'POST'])
def index():
    return 'index'

user.py内容如下

from app.views import user
from flask import session, redirect


# 让url前缀为 user的请求都要求登录
@user.before_request
def press_request(*args, **kwargs):
    user = session.get('user_info')
    if user:
        return None
    return redirect('/login/')


@user.route('/', methods=['GET', 'POST'])
def user_index():
    return 'user'

account.py内容如下

from app.views import account
from flask import request, render_template, redirect, session


@account.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == "GET":
        return render_template('login.html')
    else:
        user = request.form.get("user")
        pwd = request.form.get("pwd")
        if user == 'zdz' and pwd == '123':
            session['user_info'] = user
            # return redirect('http://www.zhengdazhi.com')
            return redirect('/user/')
        return render_template('login.html', error='用户名或密码错误')


# 为account这个蓝图下的url前缀的请求做扩展
@account.before_request
def press_request(*args, **kwargs):
    print("来了")

在app/templates目录下创建login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>用户登录</h1>
        <form method="post">
            <input type="text" name="user">
            <input type="text" name="pwd">
            <input type="submit" value="登录">{{error}}
        </form>
</body>
</html>

发表评论