In Django humanize naturalday filter, you could not get the time part since a programming issue, If you apply it like: {% load humanize %} {{ response.date_submitted|naturalday:"Y/n/j H:i" }}
You can get 'today', 'yesterday', 'tomorrow' or empty, but you can not get such as '2009/9/10 11:56'
So I change it like above code. Now you can get 'today', 'yesterday', 'tomorrow' or '2009/9/10 11:56'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from django.utils.translation import ungettext, ugettext as _
from django.utils.encoding import force_unicode
from django import template
from django.template import defaultfilters
from datetime import date
import re
register = template.Library()
def mynaturalday(value, arg=None):
"""
For date values that are tomorrow, today or yesterday compared to
present day returns representing string. Otherwise, returns a string
formatted according to settings.DATE_FORMAT.
"""
try:
value1 = date(value.year, value.month, value.day)
except AttributeError:
# Passed value wasn't a date object
return value
except ValueError:
# Date arguments out of range
return value
delta = value1 - date.today()
if delta.days == 0:
return _(u'today')
elif delta.days == 1:
return _(u'tomorrow')
elif delta.days == -1:
return _(u'yesterday')
return defaultfilters.date(value, arg)
register.filter(mynaturalday)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
- Help text hyperlinks by sa2812 11 months ago
Comments
Please login first before commenting.