目录
-
安装
pip install flask-bootstrap -
使用
from flask_bootstrap import Bootstrapapp = Flask(__name__)bootstrap = Bootstrap(app) #实例化Bootstrap -
定制
{# base.html #} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> {% block title %} <title>{{ title }}</title> {% endblock title %} {# 外链的css #} {% block css %} {% endblock css %} {# 外链js #} {% block js %} {% endblock js %} </head> <body> <table> <tr><td>11</td><td>22</td><td>33</td></tr> </table> {# 导航栏 #} {% block nav %} <h2>京东</h2> {% endblock %} {# 内容 #} {% block content %} {% endblock %} {# 页脚 #} {% block footer %} {% endblock footer %} </body> </html>
-
静态资源
目录 project/ static/ img/ abc.jpeg templates/ 引用静态资源 <img src="{{ url_for('static',filename='img/abc.jpeg',_external=True) }}" alt=""> #_external生成绝对url路径
-
全局对象
from flask import current_app @app.context_processor def appinfo(): return dict(appname=current_app.name) import time @app.context_processor def get_current_time(): def get_time(timeFormat="%b %d, %Y - %H:%M:%S"): return time.strftime(timeFormat) return dict(current_time=get_time)
<!doctype html> <title>Hello Sample</title> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <h1>Hello World!</h1> <p>Request URL: {{ request.url }}</p> <p>User: {{ session.user }}</p> <p>DB: {{ g.db }}</p> <p>Current App is: {{ appname }}</p> <p>Current Time is: {{ current_time() }}</p> <p>Current Day is: {{ current_time("%Y-%m-%d") }}</p>