Django按月连接
问题描述:
我无法搞清楚如何在我正在创建的博客上创建链接。我希望我的索引页可以链接到按年/月分组的博客文章。Django按月连接
这是我的观点,将显示所有按月/年的博客文章(网址看起来像十分之二千零十四如果我想看到从2014年10月的博客文章):
def month(request, year, month):
month_entry_list = Entry.objects.filter(pub_date__year=year, pub_date__month=month)
return render(request, 'blog/list_post_by_month.html', {
'month_entry_list': month_entry_list
})
这里是我的索引视图:
def index(request):
# latest_entry_list = Entry.objects.order_by('-pub_date')[:5]
entries = Entry.objects.all().order_by('-pub_date')
latest_entry_list = entries[:5]
template = loader.get_template('blog/index.html')
context = RequestContext(request, {
'latest_entry_list': latest_entry_list
})
return HttpResponse(template.render(context))
我对如何解决这个问题有一个想法,但我不确定它是否是最优的。我是否应该查询数据库以获取所有年/月组合的列表,并使用它在索引模板中创建链接?
答
也许你可以保留相关月份年在服务器端缓存(https://docs.djangoproject.com/en/dev/topics/cache/),只是通过附加给它更新它偶尔(即只检查自上个月有一则留言。)