In my blogging app I need a structure (created as a variable in context processor) that will store months number and corresponding year of 5 consecutive months till current one. So if current month is december, we will have year: 2010 and months: 12,11,10,9,8. If month will be january we will have years 2010: months: 1 and years: 2009 months: 12, 11, 10, 9 . My goal is to show an archive in the following form:
- 2010
- January
- 2009
- December
- November
- October
- September
How to create it and what structure should I use ? And then how to show it ? I think I need some nested structure but which will be possible to render in django < 1.2 ?
I've started it on my own but got completely lost at some point :
now = datetime.datetime.now()
years = []
months = []
archive = []
if now.month in range(5, 12, 1):
months = range(now.month, now.month-5, -1)
if months:
years = now.year
else:
diff = 5 - now.month
for i in range(1, now.month, 1):
archive.append({
"month": i,
"year": now.year,
})
for i in range(0, diff, 1):
tmpMonth = 12 - int(i)
archive.append({
"month": tmpMonth,
"year": now.year-1,
})
if archive:
years = [now.year, now.year-1]