问题
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.
I want to use WeekArchiveView to display a week's item list.
There's my urls.py
entry:
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?
回答1:
Urls like /items/
or /items/2011/
wouldn't match your regexp because \d{4}
means exactly 4 digits.
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()),
(Using isocalendar to get the week number).
来源:https://stackoverflow.com/questions/7664590/how-do-i-display-most-recent-week-items-by-default-with-weekarchiveview