NoDinner NoDinner - 3 months ago 17
Python Question

Can't use flask-moment to show the time on webpage

I want to use the Flask-moment to show the time on the web page when I visit the index.html
I've used the bootstrap up, and used the moment.js,but it still doesn't work.
Please give some help , thanks !

my main 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())


This is my base.html which extends from the bootstrap

{% 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 %}


The last is the 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>


enter image description here

Answer

If index.html only contains those two line, which is clearly reflects the output you got on the screen-shot, then you will need to extend it from base.html:

{% extends 'base.html' %}

{% block title %}Flasky - Home page{% endblock %}

{% block page_content %}
<p>The local date and time is {{moment(current_time).format('LLL')}}.</p>
<p>That was {{moment(current_time).fromNow(refresh=True)}}</p>   
{% endblock %}
Comments