I am new to Django and started the application, I made models, views, templates, but I want to add some kind of archive to the bottom of the page, something like http://www.flickr.com/photos/ionutgabriel/3990015411/ .
Therefore, I want to list all the years and next to them all the months from that year. Months that have posts to be links and not. I also want to translate the names of the months, because I need them in Romanian.
What i have done so far:
in my opinion:
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 years or by anything, and also I don’t know how to add all the months (names), I want them to be 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 Dec 2007 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec
with reference to months that have entries.
Thanks for the help!
ps sorry for my english
LE: Maybe I put the question wrong, I know how to get the dates, but I don’t know how to format them to look 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 Dec 2007 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')
from
{{ archives }} in the template there 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 tried the loop:
{% for archive in archives %} {{ archive }} <br /> {% endfor %}
and received:
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 I tried something like this:
{% for archive in archives %} {{ archive|date:"Y: m" }} <br /> {% endfor %}
and received:
2009: 10 2009: 09 2008: 10 2007: 10
Here I am stuck and do not know how to format the data, so I can get reporting years with all months and only months that have entries that will be links ...
Any ideas?
Thank you in advance!