flask_moment and flask_bootstrap的使用

  1. 现在app的init中导入这两个工具类,并且注册到app中

    import os
    from flask import Flask,request
    from flask_sqlalchemy import SQLAlchemy
    from flask_wtf.csrf import CSRFProtect
    from werkzeug.utils import import_string
    from config import config
    from flask_moment import Moment
    from flask_bootstrap import Bootstrap
    moment = Moment()
    db = SQLAlchemy()
    csrf = CSRFProtect()
    bootstrap = Bootstrap()
    def create_app(config_name):
    
        # 参加Flask应用程序实例
        app = Flask(__name__)
        config_mode = config[config_name]
        app.config.from_object(config_mode)
        db.init_app(app)
        csrf.init_app(app)
        moment.init_app(app)
        bootstrap.init_app(app)
        filenames = os.listdir("app/resources")
        for filename in filenames:
            if os.path.isdir("app/resources/"+filename) and os.path.exists('app/resources/'+filename+'/__init__.py'):
                bp = import_string('app.resources.'+filename+':'+filename)
                app.register_blueprint(bp)
        return app,db
    
  2. 在视图函数中把要传的参数传给模板

    @user.route("/temp")
    def template():
        return render_template("user/index.html", current_time = datetime.utcnow())
    
  3. 在模板中继承bootstrap,加载moment模块

{% extends "bootstrap/base.html" %}
{% block scripts %}
    {{ super() }}
    {{ moment.include_moment() }}
    {{ moment.lang('zh-CN') }}
{% endblock %}
{% block title %}flasky{% endblock %}
{% block content %}
{#

现在时间时: {{ moment().format('YYYY年M月D日, h:mm:ss a') }}.

#} {#

已经过去了: {{ moment().fromTime(time) }}.

#} {#

{{ moment().calendar() }}.

#}

The local date and time is {{ moment(current_time).format('LLL') }}.

That was {{ moment(current_time).fromNow(refresh=True) }}

{{ current_time }}

{% endblock %}

你可能感兴趣的:(flask_moment and flask_bootstrap的使用)