-
现在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
-
在视图函数中把要传的参数传给模板
@user.route("/temp") def template(): return render_template("user/index.html", current_time = datetime.utcnow())
在模板中继承bootstrap,加载moment模块
{% extends "bootstrap/base.html" %}
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{{ moment.lang('zh-CN') }}
{% endblock %}
{% block title %}flasky{% endblock %}
{% block content %}
{#<p>现在时间时: {{ moment().format('YYYY年M月D日, h:mm:ss a') }}.</p>#}
{#<p>已经过去了: {{ moment().fromTime(time) }}.</p>#}
{#<p>{{ moment().calendar() }}.</p>#}
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
<p>{{ current_time }}</p>
{% endblock %}