Lapin-Blanc
Lapin-Blanc

Reputation: 1985

New class based generic views and urlpatterns

with former generic views, I had something like this

link_info_dict = {
    'queryset' : Link.objects.all(),
    'date_field' : 'pub_date',
}
patterns('django.views.generic.date_based',
    url(r'^links/$', 'archive_index', link_info_dict, 'coltrane_link_archive_index'),
....
)

Now with new class based generic views, I found that the following seems to work :

from django.views.generic.dates import ArchiveIndexView
....
urlpatterns = patterns('',
url(r'^links/$', ArchiveIndexView.as_view(**link_info_dict), name='coltrane_link_archive_index'),
....
)

I'm wondering if i'm doing things the best way. Because I have to call the 'as_view' method, I have to import view first, and so I can't "factorize" the "django.views.generic.date_based". I'm actually using nearly all the date_based generic views. Is importing all those views at first and letting the patterns('' empty prefix the right approach ? If I migrate all my apps to this new style of views, I'd prefer to do things the right way :)

Thanks

Upvotes: 0

Views: 172

Answers (1)

rh0dium
rh0dium

Reputation: 7052

This looks fine - are you sure there isn't something else wrong? This lines up with the examples.

from django.views.generic.dates import ArchiveIndexView

from myapp.models import Article

urlpatterns = patterns('',
    url(r'^archive/$',
        ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
        name="article_archive"),
)

And it aligns with the documentation

Any arguments passed to as_view() will override attributes set on the class.

Upvotes: 2

Related Questions