i'm new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this http://www.flickr.com/photos/ionutgabriel/3990015411/.
So i want to list all years and next to them all the months from that year. The months who have posts to be links and other no. Also i want to translate the months names cause i need them in romanian.
What i've done so far is:
in my view:
def archive(request): arch = Post.objects.dates('date', 'month', order='DESC') archives = {} for i in arch: tp = i.timetuple() year = tp[0] month = tp[1] if year not in archives: archives[year] = [] archives[year].append(month) else: if month not in archives[year]: archives[year].append(month) return render_to_response('blog/arhiva.html', {'archives':archives})
and in my template:
{% for years, months in archives.items %} {{ years }} {% for month in months %} <a href="{{ years }}/{{ month }}">{{ month }}</a> {% endfor %} <br /> {% endfor %}
this returns something like:
2008 10 2009 10 9 2007 10
but i can't sort them at all...by year or by anything, and also i don't know how to add all months(the names), i want them like this:
2009 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 2008 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec2007 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec
with link on the months who have entries.
Thank you for your help!
p.s. sorry for my English
LE: Maybe i put the question in a wrong way, i know how to obtain dates, but i don't know how to format them to look like these:
2009 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec 2008 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec2007 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec
all i can get from arch = Post.objects.dates('date', 'month', order='DESC')
with
{{ archives }}
in template is something like:
[datetime.datetime(2009, 10, 1, 0, 0), datetime.datetime(2009, 9, 1, 0, 0),datetime.datetime(2008, 10, 1, 0, 0), datetime.datetime(2007, 10, 1, 0, 0)]
then i've tried a loop:
{% for archive in archives %}{{ archive }} <br />{% endfor %}
and got:
2009-10-01 00:00:00
2009-09-01 00:00:00
2008-10-01 00:00:00
2007-10-01 00:00:00
After that tried something like this:
{% for archive in archives %}{{ archive|date:"Y: m" }} <br />{% endfor %}
and got:
2009: 10
2009: 09
2008: 10
2007: 10
Here i'm stuck and don't know how to format the data so i can get distinct years with all the months and only the months who have entries to be links...
Any ideas?
Thank you in advance!