Django年/月的帖子档案 [英] Django Year/Month based posts archive

查看:95
本文介绍了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]
如果年份不在档案中:
档案[年] = []
档案[年] .append(月)
else:
如果月份不在档案[年]:
档案[年] .append(月)
返回render_to_response('blog / arhiva.html',{'档案':档案})

在我的模板中:

  {%多年,档案中的月份%} 
{{years}}
{%月份%}
&L t; 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 3月4月Mai Iun Iul Aug Sept Oct Octi Dec
2007 Ian Feb Mar Apr Mai Iun Iul Aug Sept Oct Noi Dec

与有条目的月份的链接。



感谢您的帮助!



p.s。抱歉我的英文



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')





{{archives}} 在模板中是这样的: p>

  [datetime.datetime(2009,10,1,0,0),datetime.datetime(2009,9,1, 0),
datetime.datetime(2008,10,1,0,0),datetime.datetime(2007,10,1,0,0)]
pre>

然后我尝试了一个循环:

  {%档案中存档%} 

{{archive}}< br />

{%endfor%}

得到:

  2009-10-01 00:00:00 
2009-09-01 00:00:00
2008-10 -01 00:00:00
2007-10-01 00:00:00

之后,尝试这样的事情:

  {%存档在档案%} 

{{archive |日期:Y:m}}< br />

{%endfor%}

得到:

  2009:10 
2009:09
2008:10
2007:10

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



任何想法?



提前谢谢!

解决方案

首先,datetime格式字符串在 django docs 中给出。我想你想要资本,而不是小写'M'。



由于你想显示一年中的所有12个月,即使只有一些帖子,我们将创建一个存档对象传递给模板。我选择使用一个字典,其中




  • 键是年份

  • 列表12 [datetime,bool] pair,其中 datetime 表示一个月,而 bool True 如果有那个月的帖子。



这是我们如何在视图中构建档案对象。

  from datetime import date 

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

档案= {}

对于我在拱中:
年= i.year
月= i.month
尝试:
档案[year] [month-1] [1] = True
除了KeyError:
#捕获KeyError,并设置该年份的列表
archiveives [year] = [[date ,m,1),False] for x in xrange(1,13)]
archives [year] [month-1] [1] = True

return render_to_res ponse('blog / arhiva.html',
{'archives':sorted(archives.items(),reverse = True)})

在模板中,我们循环浏览每年的月份,并在适当的情况下显示链接。

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ {$ $ $ $ $ $ $ $ $ $ $ $ $ $ { }< a href =/ {{month.year}} / {{month.month}} /> {%endif%} 
{{month | date:M}}
{%if has_link%}< / a> {%endif%}
{%endfor%}
{%endfor%}

我没有检查所有的代码,所以可能会有几个错误。最好使用网址模板标签链接,而不是硬编码url格式。我有一种感觉,我的答案可能过于复杂,但我花了一段时间打字,所以我可以和世界分享。






国际化



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



说完,如果你想显示这几个月是罗马尼亚语,这是一个丑陋的方式来做到这一点。



首先,在视图中将以下行添加到归档功能的顶部。

  rom_months = ['Ian','Feb','Mar','Apr','Mai','Iun' ,
'Iul','Aug','Sept','Oct','Noi','Dec']

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

 档案[year] = [[date ,k + 1,1),False,rom] for k,rom in enumerate(rom_months)] 

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

  ... 
{%for month,has_link,rom_month in month_list% }
{%if has_link%}< a href =/ {{month.year}} / {{month.month}} /> {%endif%}
{{rom_month} }
...


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 Dec
   2007 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 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')

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!

解决方案

Firstly, the datetime format strings are given in the django docs. I think you want capital instead of lowercase 'M'.

Since you want to display all 12 months of a year, even if only some have posts, we'll create an archives object to pass to the template. I've chosen to use a dictionary where

  • the keys are the years
  • the values are a list of 12 [datetime, bool] pairs, where datetime represents a month, and bool is True if there are posts for that month.

Here's how we build the archives object in the view.

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)})

In the template, we loop through the months for each year, and display the link if appropriate.

{% 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 %}

I haven't checked all the code so there might be a couple of bugs. It would be better to use the url template tag for the link, instead of hardcoding the url format. I have a feeling my answer might be overly complicated, but I've spent a while typing it up, so I may as well share it with the world.


Internationalization

I haven't used the internationalization features of Django, so I can't really help with the translation. I recommend you have a look at the documentation, and ask another question if there's a particular bit you don't understand.

Having said that, if you want to display the months is Romanian only, here's an ugly way to do it.

First, add the following line to the top of your archive function in the view.

rom_months = ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun', 
              'Iul', 'Aug', 'Sept', 'Oct', 'Noi', 'Dec']

Then substitute the following line into your view

archives[year]=[[date(y,k+1,1),False,rom] for k, rom in enumerate(rom_months)]

Finally substitute the following into the template

...
{% for month, has_link, rom_month in month_list %}
  {% if has_link %}<a href="/{{ month.year }}/{{ month.month }}/">{% endif %}
  {{ rom_month }}
...

这篇关于Django年/月的帖子档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆