【发布时间】:2016-08-07 12:31:05
【问题描述】:
我想在访问 index.html 时使用 Flask-moment 在网页上显示时间 我已经使用了引导程序,并使用了 moment.js,但它仍然无法正常工作。 请大家帮忙,谢谢!
我的主要py
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask import request
from datetime import datetime
app = Flask(__name__)
bootstrap=Bootstrap(app)
moment=Moment(app)
@app.route('/')
def index():
return render_template('index.html',current_time=datetime.utcnow())
这是我从引导程序扩展而来的 base.html
{% extends "bootstrap/base.html" %}
{%block head%}
{{super()}}
<link rel='shortcut icon' href="{{url_for('static',filename='favicon.ico')}}" type='image/x-icon'>
<link rel='icon' href="{{url_for('static',filename='favicon.ico')}}" type="image/x-icon">
{% block scripts %} #Here I use the bootstrap
{{ super() }} #But I dont' whether it works or not
{{ moment.include_moment() }}
{% endblock %}
{%endblock%}
{% block title %}Flasky{% 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="/">Flasky</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 %}
最后是index.html
<p>The local date and time is {{moment(current_time).format('LLL')}}.</p>
<p>That was {{moment(current_time).fromNow(refresh=True)}}</p>
【问题讨论】:
-
你还需要包含 moment jquery 文件:'{{ moment.include_jquery() }}'。
-
好的,我试试看...因为我正在阅读flask web development book ...作者说如果我使用bootstrap,那意味着我已经包含了jquery..所以我没有包括它。无论如何我会尝试一下,谢谢。
-
它不起作用.....:(有什么地方我可以看到moment和jquery是否起作用???
-
当你从模板中省略'current_time'时,你能看看它是否有效吗?
-
你的意思是这样??我试过了,但没用......
本地日期和时间是 {{moment().format('LLL')}}。
那是 {{moment ().fromNow(refresh=True)}}