Browse Source

Chapter 3: Dates and times with Flask-Moment (3e)

master
T. Meissner 6 years ago
parent
commit
5ffd385c15
3 changed files with 12 additions and 1 deletions
  1. +5
    -1
      hello.py
  2. +5
    -0
      templates/base.html
  3. +2
    -0
      templates/index.html

+ 5
- 1
hello.py View File

@ -1,9 +1,13 @@
from datetime import datetime
from flask import Flask, render_template from flask import Flask, render_template
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from flask_moment import Moment
app = Flask(__name__) app = Flask(__name__)
bootstrap = Bootstrap(app) bootstrap = Bootstrap(app)
moment = Moment(app)
@app.errorhandler(404) @app.errorhandler(404)
@ -18,7 +22,7 @@ def internal_server_error(e):
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html')
return render_template('index.html', current_time=datetime.utcnow())
@app.route('/user/<name>') @app.route('/user/<name>')


+ 5
- 0
templates/base.html View File

@ -34,4 +34,9 @@
<div class="container"> <div class="container">
{% block page_content %}{% endblock %} {% block page_content %}{% endblock %}
</div> </div>
{% endblock %}
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %} {% endblock %}

+ 2
- 0
templates/index.html View File

@ -6,4 +6,6 @@
<div class="page-header"> <div class="page-header">
<h1>Hello World!</h1> <h1>Hello World!</h1>
</div> </div>
<p>The local date and time is {{ moment(current_time).format("MMMM Do YYYY, HH:mm:ss")}}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
{% endblock %} {% endblock %}

Loading…
Cancel
Save