Flask-Moment
Flask-Moment is an extension for the Flask web application framework that simplifies the formatting and rendering of dates and times in Jinja2 templates using the client-side JavaScript library moment.js. As of its latest release, 1.0.6, the library remains active, with the most recent version released on May 28, 2024. It typically sees a few releases per year, primarily for maintenance and minor enhancements.
Common errors
-
ModuleNotFoundError: No module named 'flask_moment'
cause The `flask-moment` package is not installed or the Python environment where the application is running does not have it accessible. This can also occur if `flask_moment` is misspelled (e.g., `flask_Moment`).fixEnsure the package is correctly installed: `pip install flask-moment`. If using a virtual environment, ensure it is activated. Check for correct casing in the import statement: `from flask_moment import Moment`. -
ImportError: No module named 'flask.ext.moment'
cause Using an outdated import path for Flask extensions. The `flask.ext.` prefix was removed in Flask 1.0.fixUpdate your import statement to the modern form: `from flask_moment import Moment`. -
Flask-Moment not displaying anything on template (or showing raw `datetime` objects)
cause The JavaScript assets required by Flask-Moment are not being loaded in the browser. This usually happens if `{{ moment.include_moment() }}` is missing from the template or placed incorrectly (e.g., after the `<body>` tag or within a separate block that isn't rendered).fixEnsure `{{ moment.include_moment() }}` is present within the `<head>` section of your base template (or any template using Flask-Moment) to ensure the client-side JavaScript is initialized. -
Moment.js CDN returning 404 errors in the browser console
cause Older versions of Flask-Moment might reference specific `moment.js` versions from CDNs that are no longer available or have changed their asset paths. This was a known issue with Flask-Moment 0.10 trying to load moment.js 2.26.0.fixUpgrade to the latest version of `flask-moment` (`pip install --upgrade flask-moment`). The `include_moment()` function should automatically reference a current and stable version of `moment.js`. If issues persist, consider using the `version` argument in `include_moment()` to specify a known working version, or provide a `local_js` path to a self-hosted `moment.js` file.
Warnings
- breaking Starting with Flask-Moment 1.0.0, the `moment.include_jquery()` function was removed. jQuery is no longer a dependency of `flask-moment` itself.
- gotcha The `refresh=True` argument for rendering functions like `format()` will only refresh the *client-side* display, not re-evaluate the Python `datetime` object passed from the server. If you pass a fixed `datetime` object, `refresh=True` will not make it update like a live clock, but only update relative formats (e.g., 'X seconds ago').
- gotcha Flask-Moment generates inline JavaScript for its functionality, which can be blocked by strict Content Security Policies (CSP). This may result in 'Refused to execute inline script' errors.
- deprecated The underlying moment.js library, which Flask-Moment relies on, is now considered a legacy project in maintenance mode. Its developers recommend that new projects avoid it and choose modern alternatives due to bundle size, mutability issues, and a different API design philosophy.
Install
-
pip install flask-moment
Imports
- Moment
from flask.ext.moment import Moment
from flask_moment import Moment
Quickstart
from flask import Flask, render_template
from flask_moment import Moment
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a-very-secret-key'
moment = Moment(app)
@app.route('/')
def index():
return render_template('index.html', current_time=datetime.utcnow())
# --- In templates/index.html ---
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8">
# <title>Flask-Moment Example</title>
# {{ moment.include_moment() }}
# </head>
# <body>
# <p>The current time is: {{ moment(current_time).format('MMMM Do YYYY, h:mm:ss a') }}.</p>
# <p>A relative time: {{ moment(current_time).fromNow() }}.</p>
# </body>
# </html>