基于Django年/月的帖子存档

cig*_*212 6 python django

我是Django的新手并启动了一个应用程序,我做了模型,视图,模板,但我想在页面底部添加一些存档,如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}) 
Run Code Online (Sandbox Code Playgroud)

在我的模板中:

    {% for years, months in archives.items %} 
                    {{ years }} 
                    {% for month in months %} 
                   <a href="{{ years }}/{{ month }}">{{ month }}</a> 
                    {% endfor %} 
            <br /> 
                {% endfor %} 
Run Code Online (Sandbox Code Playgroud)

这会返回如下内容:

       2008               10 
       2009               10               9 
       2007               10 
Run Code Online (Sandbox Code Playgroud)

但是我根本无法对它们进行排序......年份或任何事情,而且我也不知道如何添加所有月份(名称),我希望它们像这样:

   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
Run Code Online (Sandbox Code Playgroud)

有条目的月份链接.

谢谢您的帮助!

抱歉我的英语

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
Run Code Online (Sandbox Code Playgroud)

我能得到的一切 arch = Post.objects.dates('date', 'month', order='DESC')

{{ 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)]
Run Code Online (Sandbox Code Playgroud)

然后我尝试了一个循环:

{% for archive in archives %}

{{ archive }} <br />

{% endfor %}
Run Code Online (Sandbox Code Playgroud)

得到了:

2009-10-01 00:00:00 
2009-09-01 00:00:00 
2008-10-01 00:00:00 
2007-10-01 00:00:00 
Run Code Online (Sandbox Code Playgroud)

之后尝试过这样的事情:

{% for archive in archives %}

{{ archive|date:"Y: m" }} <br />

{% endfor %}
Run Code Online (Sandbox Code Playgroud)

得到了:

2009: 10 
2009: 09 
2008: 10 
2007: 10 
Run Code Online (Sandbox Code Playgroud)

在这里我被困住了,不知道如何格式化数据,所以我可以在所有月份获得不同的年份,只有几个月有条目的链接...

有任何想法吗?

先感谢您!

Ala*_*air 12

首先,日期时间格式字符串在django文档中给出.我认为你想要资本而不是小写的'M'.

由于您希望显示一年中的所有12个月,即使只有一些有帖子,我们也会创建一个archives对象以传递给模板.我选择使用字典

  • 钥匙是岁月
  • 值是12的列表[datetime, bool]对,其中datetime代表了一个月,bool就是True如果有当月的帖子.

这是我们archives在视图中构建对象的方式.

from datetime import date

def archive(request):
    arch = Post.objects.dates('date', 'month', order='DESC')

    archives = {}

    for i in arch:
        year = i.year
        month = i.month
        try:
            archives[year][month-1][1]=True
        except KeyError:
            # catch the KeyError, and set up list for that year
            archives[year]=[[date(y,m,1),False] for m in xrange(1,13)]
            archives[year][month-1][1]=True

    return render_to_response('blog/arhiva.html', 
              {'archives':sorted(archives.items(),reverse=True)})
Run Code Online (Sandbox Code Playgroud)

在模板中,我们遍历每年的月份,并在适当的时候显示链接.

{% for year, month_list in archives %}
  {{ year }} archives: 
  {% for month, has_link in month_list %}
    {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
      {{ month|date:"M" }}
    {% if has_link %}</a>{% endif %}
  {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我没有检查所有代码,因此可能存在一些错误.最好使用url模板标记作为链接,而不是硬编码url格式.我有一种感觉,我的答案可能过于复杂,但我花了一段时间打字,所以我不妨与世界分享.


国际化

我没有使用Django的国际化功能,所以我无法真正帮助翻译.我建议您查看文档,并询问另一个问题,如果您有一点不明白.

话虽如此,如果你想显示月份只是罗马尼亚语,这是一个丑陋的方式来做到这一点.

首先,在视图中将以下行添加到归档函数的顶部.

rom_months = ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun', 
              'Iul', 'Aug', 'Sept', 'Oct', 'Noi', 'Dec']
Run Code Online (Sandbox Code Playgroud)

然后将以下行替换为您的视图

archives[year]=[[date(y,k+1,1),False,rom] for k, rom in enumerate(rom_months)]
Run Code Online (Sandbox Code Playgroud)

最后将以下内容替换为模板

...
{% for month, has_link, rom_month in month_list %}
  {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
  {{ rom_month }}
...
Run Code Online (Sandbox Code Playgroud)