Flask——flask-bootstrap

目录

  1. 安装
  2. 使用
  3. 定制
  4. 静态资源
  5. 全局对象

 

  1. 安装


    pip install flask-bootstrap
  2. 使用


    from flask_bootstrap import Bootstrap
    app = Flask(__name__)
    bootstrap = Bootstrap(app) #实例化Bootstrap
  3. 定制

    {# 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>
  4. 静态资源

    目录
    project/
     static/
     img/
     abc.jpeg
     templates/
    引用静态资源
    <img src="{{ url_for('static',filename='img/abc.jpeg',_external=True) }}"
    alt="">
    #_external生成绝对url路径

     

  5. 全局对象

    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>

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值