默认情况下,如何使用WeekArchiveView显示最近一周的项目? [英] How do I display most recent week items by default with WeekArchiveView?

查看:196
本文介绍了默认情况下,如何使用WeekArchiveView显示最近一周的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对几个文档感到惊讶基于类的通用视图有。

任何比琐碎样本复杂得多的事情都要通过猜测,审判和错误来完成。

I'm astonished by how little documentation on class-based generic views there is.
Anything slightly more complex than a trivial sample has to get done through guesswork, trial and error.

我想使用 WeekArchiveView 显示一周的项目列表。

I want to use WeekArchiveView to display a week's item list.

有我的 urls.py 条目:

url(r'^items/(?P<year>\d{4})/week/(?P<week>\d{1,2})/$', ItemWeekArchiveView.as_view())

当没有,我收到一个错误页面。

我希望他们默认情况下等于今天的年和周。

When no year or week is specified, I get an error page.
I want them to equal today's year and week by default.

在这里调整适当的地方是什么?我应该引入另一个混合并覆盖一个方法吗?

What is the right place for tweak here? Should I introduce another mixing and override a method?

推荐答案

URL像 / items / / items / 2011 / 不符合您的正则表达式,因为 \d {4} 4位数字。

Urls like /items/ or /items/2011/ wouldn't match your regexp because \d{4} means exactly 4 digits.

您可能应该为这两种情况指定另外两个URL条目:

You probably should specify two another url entries for both cases:

url(r'^items/$', AchievementListView.as_view(
    year=str(date.today().year), week=str(date.today().isocalendar()[1])
    )),
url(r'^items/(?P<year>\d{4})/week/(?P<week>\d{1,2})/$', ItemWeekArchiveView.as_view()),

使用 isocalendar 获取周数) 。

(Using isocalendar to get the week number).

这篇关于默认情况下,如何使用WeekArchiveView显示最近一周的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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