- Author:
- mackenzie_kearl
- Posted:
- February 16, 2008
- Language:
- Python
- Version:
- .96
- Score:
- 2 (after 2 ratings)
template tag to format two dates as a date range
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from django import template
register = template.Library()
class DateRangeNode(template.Node):
def __init__(self, start_date, end_date, format_string):
self.start_date = template.Variable(start_date)
self.end_date = template.Variable(end_date)
self.format = {}
if not len(format_string) == 0:
format_string = format_string.encode('utf-8').strip("\"")
self.format['day'],self.format['month'],self.format['year'] = format_string.split()
else:
self.format['day'],self.format['month'],self.format['year'] = "%d","%m","%Y"
def render(self, context):
try:
start_date = self.start_date.resolve(context)
end_date = self.end_date.resolve(context)
except template.VariableDoesNotExist:
return ''
start_format = ""
end_format = ""
if start_date.day == end_date.day:
end_format = self.format['day']
else:
start_format = self.format['day']
end_format = self.format['day']
if start_date.month == end_date.month:
end_format += " "+self.format['month']
else:
start_format += " "+self.format['month']
end_format += " "+self.format['month']
if start_date.year == end_date.year:
end_format += " "+self.format['year']
else:
start_format += " "+self.format['year']
end_format += " "+self.format['year']
print start_format
print end_format
return start_date.strftime(start_format)+" - "+end_date.strftime(end_format)
def do_date_range(parser, token):
""" formats two dates as a date range
eg.
January 1st 2009 to January 5th 2009
would result in:
1 - 5 January 2009
template usage:
{% date_range start_date end_date [format string] %}
"""
chunks = token.split_contents()
if not len(chunks) >= 3:
raise template.TemplateSyntaxError, "%r tag requires two or three arguments" % token.contents.split()[0]
if not len(chunks) <=4 :
raise template.TemplateSyntaxError, "%r tag requires two or three arguments" % token.contents.split()[0]
if len(chunks) == 4:
format = chunks[3]
else:
format = ""
return DateRangeNode(chunks[1],chunks[2],format)
register.tag('date_range',do_date_range)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
This is very nice, thank-you. I adapted this to to use Django's date formatting rather than strftime formatting, since it has more options. Basically, I added
and then replaced the
return
statement inrender()
withYou also have to remove
%
from the default option.#
Please login first before commenting.