Django naturaltime tag

1k Views Asked by At

I want to receive time difference,so I have added 'django.contrib.humanize' to Installed Apps, and '{% load humanize %}' to my template, and use it in as below:

{% for notification in request.session.notifications %}
       <li> <div class="media">
            <div class="media-left">
            <div class="media-object">
                 <img data-src="holder.js/50x50?bg=cccccc" class="img-circle" alt="50x50" style="width: 50px; height: 50px;" src="{% static 'img/profile.png' %}" data-holder-rendered="true">
             </div>
             </div>
             <div class="media-body">
             <strong class="notification-title"><a href="#">{{ notification.fields.actor_object_id }} {{ notification.fields.verb }} {{ notification.fields.target_object_id }}</a></strong>
             <div class="notification-meta">
                  <small class="timestamp">{{ notification.fields.timestamp|naturaltime }}</small>
            </div>
            </div>
            </div> 
       </li>
{% endfor %}

but result is same again: 2017-01-18T10:59:11Z

1

There are 1 best solutions below

0
On

As Resley said, timestamp field converted to dictionary when data serialized to JSON. I created custom tag and used it in my template.

from django import template
import dateutil.parser

register = template.Library()

@register.filter(name='to_date') 
def to_date(value):
    print type(dateutil.parser.parse(value))
    return dateutil.parser.parse(value)

then in template, changed the line as following:

<small class="timestamp">{{ notification.fields.timestamp|to_date|naturaltime }}</small>