-
現(xiàn)在app的init中導(dǎo)入這兩個工具類,并且注冊到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應(yīng)用程序?qū)嵗? 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
-
在視圖函數(shù)中把要傳的參數(shù)傳給模板
@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>現(xiàn)在時間時: {{ moment().format('YYYY年M月D日, h:mm:ss a') }}.</p>#}
{#<p>已經(jīng)過去了: {{ 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 %}