从零开始学Flask框架-004
Flask-Moment
pip install flask-moment
除了moment.js,Flask-Moment 还依赖jquery.js。但是Bootstrap 已经引入了jquery.js,因此只需引入moment.js 即可。
templates/base.html:引入moment.js 库
{% 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 %}
templates/index.html:
{% 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>
index.py
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()
本文来自博客园,作者:江雪独钓翁,转载请注明原文链接:https://www.cnblogs.com/zhouwp/p/11911707.html