Home > Mobile >  Is there a way to execute datetime in the base.html file with flask?
Is there a way to execute datetime in the base.html file with flask?

Time:12-22

Currently I have a block date in the base.html page. In the routes.py I then execute the function datetime.today().strftime('%Y-%m-%d %H:%M'). Then i pass it to the appropriate page for example start.html. But this is how I have to do it for every other page.

base.html

<!doctype html>
<html lang="en">
   <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
       
       {% block css %}

       {% endblock %}
       
      
      <title>
          {% block title %}

          {% endblock %}
      </title>
   </head>

   <body>
        <!--date-->
        {% block date %}

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

routes.py

from page import app
from flask import render_template, redirect, url_for,request
from datetime import datetime



@app.route('/')#decorater
@app.route('/start')
def Start_Page():
  date = datetime.today().strftime('%Y-%m-%d %H:%M')
  return render_template('start.html',date=date)

start.html

{% extends 'base.html' %}

{% block title %}
    Startseite
{% endblock %}

{% block css %}
    <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/style.css')}}"/>
{% endblock %}

{% block date %}
   {{date}}
{% endblock %} 

Is there a way how I can include this function:datetime.today().strftime('%Y-%m-%d %H:%M')datetime.today().strftime('%Y-%m-%d %H:%M') in the base.html?

CodePudding user response:

To do this, you can define a function that gets the current date and time and add it to the template context. Then, you can register this function as a template context processor using the app.context_processor decorator in your routes.py file.

from page import app
from flask import render_template
from datetime import datetime

def get_current_date():
    return {'current_date': datetime.today().strftime('%Y-%m-%d %H:%M')}

app.context_processor(get_current_date)

@app.route('/')#decorater
@app.route('/start')
def Start_Page():
  return render_template('start.html')

Then, in your base template, you can access the current_date variable as follows:

<!doctype html>
<html lang="en">
   <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
       
       {% block css %}
       {% endblock %}       
      
      <title>
          {% block title %}

          {% endblock %}
      </title>
   </head>
   <body>
        <!--date-->
        {% block date %}
            {{current_date}}
        {% endblock %}
    </body>
</html>

Update:

If you want to always refresh the datetime without the user reloading the page then you should use JavaScript:

<script>
    function updateDate() {
        var currentDate = new Date();
        var formattedDate = currentDate.toLocaleDateString()   " "   currentDate.toLocaleTimeString();
        document.getElementById("current-date").innerHTML = formattedDate;
    }
    setInterval(updateDate, 60000);
</script>

and then embed it in your base.html the following way:

<!--date-->
<div id="current-date">
   {% block date %}
   {% endblock %}
</div>
  • Related