Django tag template for date

abrunet

from an API, I got a date which is rendered like this with {{ value }}: 2014-03-25T15:51:29Z

I'd like to display it in a more fancy way but with the tag {{ value|date:'D d M Y' }}, nothing is displayed... Should I somehow specify the input date format? Cheers

Yuval Adam

It seems like value isn't a datetime object but rather a string. You'll need to convert it in your view before you can process it in the template.

There are several ways to do this, see How to parse ISO formatted date in python? for many examples.

So if your view has:

import re
from datetime import datetime 

def my_view(request):
    date_str = '2014-03-25T15:51:29Z'
    # must convert to datetime object
    date = datetime(*map(int, re.split('[^\d]', date_str)[:-1]))
    return render_to_response('template.html', {'date': date})

Only then you can do:

{{ date|date:'D d M Y' }}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related