我是 Django 新手并启动了一个应用程序,我做了模型,
视图、模板,但我想在底部添加某种存档
页面的内容,类似这样http://www.flickr.com/photos/ionutgabriel/3990015411/ http://www.flickr.com/photos/ionutgabriel/3990015411/.
所以我想列出所有年份以及当年的所有月份。那个有帖子的人要链接,其他没有。我还想翻译月份名称,因为我需要罗马尼亚语。
到目前为止我所做的是:
在我看来:
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})
在我的模板中:
{% for years, months in archives.items %}
{{ years }}
{% for month in months %}
<a href="{{ years }}/{{ month }}">{{ month }}</a>
{% endfor %}
<br />
{% endfor %}
这会返回类似:
2008 10
2009 10 9
2007 10
但我根本无法对它们进行排序...按年份或任何内容,而且我也不知道如何添加所有月份(名称),我希望它们像这样:
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
带有有条目的月份的链接。
感谢您的帮助!
附注对不起我的英语不好
LE:也许我以错误的方式提出了问题,我知道如何获取日期,但我不知道如何将它们格式化为如下所示:
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
我能从中得到的一切arch = Post.objects.dates('date', 'month', order='DESC')
with
{{ archives }}
在模板中是这样的:
[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)]
然后我尝试了一个循环:
{% 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
之后尝试了这样的事情:
{% for archive in archives %}
{{ archive|date:"Y: m" }} <br />
{% endfor %}
and got:
2009: 10
2009: 09
2008: 10
2007: 10
在这里我被困住了,不知道如何格式化数据,这样我就可以得到所有月份的不同年份,并且只有有条目作为链接的月份......
有任何想法吗?
先感谢您!