内容简介:文件目录结构如下.├── app
通过多个蓝图将不同功能的视图函数放到不同的模块文件下
文件目录结构如下
.
├── 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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 网站模板 | 现代时尚创新创意投资组合HTML5模板设计
- ReportLibrary 报表模板库新增 21 张报表模板,加入报表导出功能!
- ReportLibrary 报表模板库新增 21 张报表模板,加入报表导出功能!
- 工具集核心教程 | 第五篇: 利用Velocity模板引擎生成模板代码
- Word 模板引擎 poi-tl V1.3.0 发布,新增模板语法
- React与Vue模板使用比较(一、vue模板与React JSX比较)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Everything Store
Brad Stone / Little, Brown and Company / 2013-10-22 / USD 28.00
The definitive story of Amazon.com, one of the most successful companies in the world, and of its driven, brilliant founder, Jeff Bezos. Amazon.com started off delivering books through the mail. Bu......一起来看看 《The Everything Store》 这本书的介绍吧!