Вопрос

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?

Это было полезно?

Решение

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).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top