I know how to print out current date and time in the console, but I am trying to add the date/time as a feature on my daily task Python website using Django and Bootstrap4.
I have tried adding it as a function in my views.py file and then inserting the function in the html file of the page i want it to be on, but everything I have tried has not worked.
Here is the function I created at first in views.py:
JavaScript
x
5
1
def today():
2
"""Shows todays current time and date."""
3
today = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
4
return today
5
Then I began playing around and tried this:
JavaScript
1
6
1
def today(request):
2
"""Shows todays current time and date."""
3
today = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
4
context = {'today': today}
5
return render(request, context)
6
And this is what I tried adding to the html file:
JavaScript
1
2
1
<h4>{{ today }}</h4>
2
Update – Trying this and still does not show anything:
JavaScript
1
6
1
def today(request):
2
"""Shows todays current time and date."""
3
today = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
4
context = {'today': today}
5
return render(request, 'learning_logs/topics.html', context)
6
Advertisement
Answer
If you just want to display only in the template then you can do this
JavaScript
1
2
1
Current datetime:{% now "jS F Y H:i" %}
2