Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python [Flask-Moment] I can’t show the current time

Kaworu

Active Coder
Hi!

I am slowly learning Flask framework. When trying to show the current time (and time that had passed since the page got generated) I encountered an error.

My app.py looks like this:

Python:
from flask import Flask, render_template

from flask_bootstrap import Bootstrap

from flask_moment import Moment

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

# some other code, not important right now

My index.html in templates folder looks like this:

HTML:
<!doctype html>

<html>

<head>

<title>Flasky - index from template</title>

</head>

<body>

<h1 align="center">Index from template</h1>

<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>

<p>That was {{ moment(current_time).fromNow(refresh=True) }}.</p>

</body>

</html>

And what I get is this:
Flask-Moment.png

I dunno what I am doing wrong, being honest. I would appreciate some help.
 
Did you include this in the head tags?
{{ moment.include_moment() }}

index.html
HTML:
{% extends 'base.html' %}

{% block title %}My Site{% endblock %}
{% block content %}

<p>
    Current - {{moment().format('LLL')}}
</p>
<p>
    Passed - {{moment().fromNow(refresh=True)}}
</p>
{% endblock %}

base.html
HTML:
<!DOCTYPE html>
<html>
    <head>
        <title>{% block title %}Hi{% endblock %}</title>
        <link rel="stylesheet" href="static/css/base.css"></link>
        {{ moment.include_moment() }}
    </head>
    <body>
        <h1>{% block header %}My Website Header{% endblock %}</h1>

        {% block content %}
        
        {% endblock %}
    </body>
</html>

init.py
Python:
from flask import Flask, render_template
from flask_moment import Moment

app = Flask(__name__)
app.secret_key = 'My Secret Key'
moment = Moment(app)

@app.route('/')
def index():

    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)
 
Last edited:
Okay, so!

Before, I had {{ moment.include_moment() }} in separate {{ scripts }} block, and apparently it was not working. So, it seems, the solution was to put {{ moment.include_moment() }} in the head? And, miraciously, it works? ( YAY 😀).

But just a question - shouldn't we be putting scripts in the head? : < Because... the page loads longer? Shouldn't the scripts be put at the very end of a webpage?

At least it was what I learned of HTML in school...? But, of course, I can be wrong. And the script is finally working :3 Yay :3

Thank you a lot ;-)

EDIT: I tried to put {{ scripts }} block just below {{ head }} and it's not working. Seems like the scripts have to be put in the head? Okay, I will remember in the future ;-)
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom