One other notable commit this past week is Changeset 5877. This particular changeset helps dry up generic views a bit. The modification sets the default slug_field key to 'slug'. Why is this important? Consider how we’ve been doing standard generic date-based views:
blog_dict = {
'queryset': Article.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$', 'object_detail', dict(blog_dict, slug_field='slug')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', blog_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', blog_dict),
(r'^(?P<year>\d{4})/$', 'archive_year', blog_dict),
(r'^/?$', 'archive_index', blog_dict),
)
Note that whenever we’re dealing with a slug field and the object_detail, we need to be sure to modify the dict to include the slug_field key. With Changeset 5877, the default is to set slug_field='slug'. Now we can just specify our URLConf like so:
blog_dict = {
'queryset': Article.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$', 'object_detail', blog_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', blog_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', blog_dict),
(r'^(?P<year>\d{4})/$', 'archive_year', blog_dict),
(r'^/?$', 'archive_index', blog_dict),
)
Much cleaner.
