How do I display most recent week items by default with WeekArchiveView?

家住魔仙堡 提交于 2019-12-11 02:38:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!