Flask-Momenthtml
pip install flask-moment
除了moment.js,Flask-Moment 還依賴jquery.js。可是Bootstrap 已經引入了jquery.js,所以只需引入moment.js 便可。jquery
templates/base.html:引入moment.js 庫flask
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky{% endblock %}
{#引入moment.js 庫#}
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
{#包含導航條的程序基模板#}
{% block navbar %}
<div class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle"
data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">TEST</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
</ul>
</div>
</div>
</div>
{% endblock %}
{% block content %}
<div class="container">
{% block page_content %}{% endblock %}
</div>
{% endblock %}
{% extends "base.html" %} {% block title %}IndexTitle{% endblock %} <div class="container"> {% block page_content %} <h1>Hello Flask!</h1> <p>本地時間格式化:{{ moment(current_time).format('YYYY-MM-DD HH:mm:ss') }}.</p> <p>本地時間格式化:{{ moment(current_time).format('YYYY-MM-DD') }}.</p> <p>本地時間格式化:{{ moment(current_time).format('YYYY') }}.</p> {# {{ wtf.quick_form(form) }}#} {# 你剛輸入的姓名是:#} {# {{ name }}#} {% endblock %} </div>
from flask import Flask,render_template from flask_bootstrap import Bootstrap from flask import request,url_for from flask_moment import Moment from datetime import datetime #初始化 app = Flask(__name__) bootstrap = Bootstrap(app) # 初始化實例 moment = Moment(app) #路由,處理URL 和函數之間關係的程序稱爲路由 @app.route('/') #視圖函數,函數執行,返回結果 def index(): return render_template('index.html',current_time=datetime.utcnow()) #啓動服務器 if __name__ == '__main__': app.run()