小编cig*_*212的帖子

基于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 /> 
                {% …
Run Code Online (Sandbox Code Playgroud)

python django

6
推荐指数
1
解决办法
3440
查看次数

标签 统计

django ×1

python ×1