2017-01-24 1 views
0

Ich möchte Zeitunterschied erhalten, also habe ich "Django.contrib.humanize" zu den installierten Apps hinzugefügt, und "{% load humanize%}" in meine Vorlage, und verwenden Sie sie wie folgt:Django naturaltime Tag

{% 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 %} 

aber Ergebnis ist dasselbe wieder: 2017-01-18T10: 59: 11Z

+0

Welche Art von Feld ist 'timestamp'? – Sayse

+0

Es ist DateTimeField –

+0

Gibt es Vererbung beteiligt? wo laden Sie humanize genau? – Sayse

Antwort

0

Resley Wie gesagt, Zeitstempel-Feld zum Wörterbuch umgewandelt, wenn Daten in JSON serialisiert. Ich habe ein benutzerdefiniertes Tag erstellt und es in meiner Vorlage verwendet.

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) 

dann in Vorlage, geändert, um die Zeile wie folgt:

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