{"id":8170,"library":"flask-moment","title":"Flask-Moment","description":"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.","status":"active","version":"1.0.6","language":"en","source_language":"en","source_url":"https://github.com/miguelgrinberg/flask-moment","tags":["flask","datetime","javascript","frontend","moment.js","jinja2","web"],"install":[{"cmd":"pip install flask-moment","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core dependency as Flask-Moment is a Flask extension.","package":"Flask"}],"imports":[{"note":"The `flask.ext.` prefix was deprecated in Flask 0.11 and removed in Flask 1.0. All modern Flask extensions are imported directly from their package name.","wrong":"from flask.ext.moment import Moment","symbol":"Moment","correct":"from flask_moment import Moment"}],"quickstart":{"code":"from flask import Flask, render_template\nfrom flask_moment import Moment\nfrom datetime import datetime\n\napp = Flask(__name__)\napp.config['SECRET_KEY'] = 'a-very-secret-key'\nmoment = Moment(app)\n\n@app.route('/')\ndef index():\n    return render_template('index.html', current_time=datetime.utcnow())\n\n# --- In templates/index.html ---\n# <!DOCTYPE html>\n# <html lang=\"en\">\n# <head>\n#     <meta charset=\"UTF-8\">\n#     <title>Flask-Moment Example</title>\n#     {{ moment.include_moment() }}\n# </head>\n# <body>\n#     <p>The current time is: {{ moment(current_time).format('MMMM Do YYYY, h:mm:ss a') }}.</p>\n#     <p>A relative time: {{ moment(current_time).fromNow() }}.</p>\n# </body>\n# </html>","lang":"python","description":"Initialize the `Moment` extension with your Flask application. In your Jinja2 template, include `{{ moment.include_moment() }}` in the `<head>` section to load the necessary JavaScript libraries. Then, you can use the `moment()` function in your templates to format `datetime` objects or render current time, utilizing various moment.js formatting options."},"warnings":[{"fix":"Remove `{{ moment.include_jquery() }}` from your templates. Only `{{ moment.include_moment() }}` is required. If your application still requires jQuery, you must include it separately.","message":"Starting with Flask-Moment 1.0.0, the `moment.include_jquery()` function was removed. jQuery is no longer a dependency of `flask-moment` itself.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use `refresh=True` only with dynamic rendering functions like `fromNow()` or `fromTime()`. For a live-updating clock or similar, consider client-side JavaScript solutions or periodic AJAX calls to fetch updated `datetime` objects from the server.","message":"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').","severity":"gotcha","affected_versions":"All versions"},{"fix":"To comply with CSP, you can use `moment.include_moment(no_js=True)` and manually load `moment.js` (and optionally `moment-timezone.js`) from a trusted CDN or local source with appropriate SRI hashes. Then, you'll need to wrap the output of `moment.flask_moment_js()` in a script tag with a nonce. Refer to the `flask-moment` documentation for advanced CSP integration.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects, consider using a different Flask extension for date/time handling that integrates with a modern JavaScript date library (e.g., `date-fns`, `Luxon`). For existing projects, be aware of `moment.js`'s maintenance status and potential future limitations.","message":"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.","severity":"deprecated","affected_versions":"All versions (due to upstream library status)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure 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`.","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`).","error":"ModuleNotFoundError: No module named 'flask_moment'"},{"fix":"Update your import statement to the modern form: `from flask_moment import Moment`.","cause":"Using an outdated import path for Flask extensions. The `flask.ext.` prefix was removed in Flask 1.0.","error":"ImportError: No module named 'flask.ext.moment'"},{"fix":"Ensure `{{ 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.","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).","error":"Flask-Moment not displaying anything on template (or showing raw `datetime` objects)"},{"fix":"Upgrade 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.","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.","error":"Moment.js CDN returning 404 errors in the browser console"}]}