flask之Blueprint蓝图
2024-10-09 03:05:02
flask_Blueprint.py
'''
flask中的Blueprint蓝图:
(1)新建模块,例如Bp1.py,Bp2.py,在模块中创建蓝图实例
(2)通过app.register_blueprint()对蓝图对象进行注册
(3)如果在蓝图中设置了参数url_prefix就必须在请求的端口后路径前加上
''' from flask import Flask, Blueprint, render_template app = Flask(__name__)
app.config['DEBUG'] = True # 导入蓝图,通过app.register_blueprint()对蓝图对象进行注册
from Bp1 import Bp1
from Bp2 import Bp2 app.register_blueprint(Bp1)
app.register_blueprint(Bp2) @app.route('/base')
def base():
return render_template('index.html') if __name__ == '__main__':
app.run()
Bp1.py
'''
flask中的Blueprint蓝图类源码:
def __init__(
self,
name, # 唯一蓝图名
import_name, #当前文件名
static_folder=None, #静态文件目录
static_url_path=None, #静态文件请求路径
template_folder=None, #模板文件目录
url_prefix=None, #url请求路径前缀,如果指定,在请求时必须在端口之后路径之前加上才能正常访问
subdomain=None, #子域
url_defaults=None,
root_path=None,
cli_group=_sentinel,
) 蓝图中没有config配置和run函数,功能于一个app的分支功能模块,在主程序中通过app.register_blueprint()进行注册引入使用
''' from flask import Blueprint, render_template Bp1=Blueprint('Bp1',__name__,static_folder='staticfiles',static_url_path='/static',template_folder='html',url_prefix='/Bp1') @Bp1.route('/BluePrint')
def func1():
return 'BluePrint1' @Bp1.route('/index')
def index():
return render_template('index0.html')
Bp2.py
from flask import Blueprint
Bp2=Blueprint('Bp2',__name__,url_prefix='/Bp2') @Bp2.route('/BluePrint')
def func1():
return 'BluePrint2'
indx0.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h2>项目中指定模板文件目录html中的index页面,默认不指定为templates模板目录</h2> <div><h2>项目中指定静态文件目录staticfiles返回当前图片,默认不指定为static静态文件目录</h2>
<img src="/static/1.png" alt=""></div>
</body>
</html>
最新文章
- Jmeter实现登录bugfree、新建bug、解决bug脚本(抓包工具实现)
- CF Round #367 C题
- 【OpenWRT】【RT5350】【二】烧写OpenWrt到RT5350开发板
- Apache Shiro权限框架在SpringMVC+Hibernate中的应用
- Spring配置JNDI的解决方案
- 关于hook d3d在war3上绘图的几点疑问
- asp.net mvc razor html encoding
- dorado问题查询&;快捷键重命名
- 通过live555实现H264 RTSP直播
- Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net >; Framework >; v4.0.30319 >; Temporary ASP.NET Files
- python内置函数(2)-递归与迭代
- [ 转 ]jquery的ajax和getJson跨域获取json数据
- Python学习笔记008_类_对象_继承_组合_类相关的BIF
- Spring Boot实战笔记(六)-- Spring高级话题(多线程)
- maven打包如何跳过测试
- 批量删除MSSQL 中主外键约束
- RabbitMQ简单应用の消息持久化
- nginx的权限问题(13: Permission denied)解决办法
- 安装Java语言的jdk,配置java环境变量
- WebPage设计专业术语
热门文章
- centos7与8的区别
- T-SQL字符串函数
- 【Linux常见命令】split命令
- Linux 开发之线程条件锁那些事
- STL--priority_queue--自定义数据类型
- Linux环境下,MongoDB 3.6.10 的安装步骤,以及设置用户和密码,配置随处执行mongo命令启动客户端,以及所遇到的问题
- CF思维联系– Codeforces-988C Equal Sums (哈希)
- 数学--数论--HDU 2197 本原串 (推规律)
- [bzoj2427]P2515 [HAOI2010]软件安装(树上背包)
- 集成学习基础知识总结-Bagging-Boosting