Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Want to print the date & time for today

I want to print some sort of time stamp or some sort of function to tell what day and time it is in a template. In my views I have

time = datetime.now()

and in my template I have

{{time}}

All this does is prints out a <type 'datetime.date'> object.

like image 327
Shehzad009 Avatar asked Apr 13 '11 15:04

Shehzad009


2 Answers

if its just in the template, use now

It is {% now "f" %}
like image 145
JamesO Avatar answered Oct 14 '22 09:10

JamesO


Normally, this should work:

from datetime import datetime

def a_view(request):
    return render_to_response("a_template.html", {
        'time':datetime.now(),
        }, context_instance=RequestContext(request))

Then render the datetime object in your template:

<p>{{time}}</p>

Use the built-in date filter as described here to format your date if you like.

like image 25
aminho Avatar answered Oct 14 '22 08:10

aminho