Django - Migrating function-based generic date_based views
I used Django's generic date_based views in Django v1.1:
urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from coltrane.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('',
(r'^admin/', include(admin.site_urls)),
(r'^search/$', 'cms.search.views.search'),
(r'^weblog/$', 'django.views.generic.date_based.archive_index',entry_info_dict),
(r'^weblog/(?P<year>\d{4}/$', 'django.views.generic.date_based.archive_year', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/$', 'django.views.generic.date_based.archive_month', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(?P<day>\d{2})/$', 'django.views.generic.date_based.archive_day', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 'django.views.generic.date_based.object_detail', entry_info_dict),
(
Then, I upgraded the source code to work with Django v1.5. It raised errors when I's trying to access these generic date_based views above:
It is because in the newer version, Django 1.5, the generic function- based views are replaced by generic class-based views. So, I have to migrate the function-based views to class-based views:
urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from django.views.generic.dates import YearArchiveView
from django.views.generic.dates import MonthArchiveView
from django.views.generic.dates import DayArchiveView
from django.views.generic.dates import DateDetailView
from coltrane.models import Entry
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/?', include(admin.site.urls)),
url(r'search/$', 'cms.search.views.search'),
url(r'^weblog/$', ArchiveIndexView.as_view(model=Entry, date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/$', YearArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/$',
MonthArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
DayArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
DateDetailView.as_view(model=Entry,
date_field="pub_date")),
url(r'', include('django.contrib.flatpages.urls')),
)
References:
Django's documentation: https://docs.djangoproject.com/en/1.5/ref/class-based-views/generic-date-based/#module-django.views.generic.dates
Practical Django projects by James Bennett.
urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from coltrane.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('',
(r'^admin/', include(admin.site_urls)),
(r'^search/$', 'cms.search.views.search'),
(r'^weblog/$', 'django.views.generic.date_based.archive_index',entry_info_dict),
(r'^weblog/(?P<year>\d{4}/$', 'django.views.generic.date_based.archive_year', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/$', 'django.views.generic.date_based.archive_month', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(?P<day>\d{2})/$', 'django.views.generic.date_based.archive_day', entry_info_dict),
(r'^weblog/(?P<year>\d{4}/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 'django.views.generic.date_based.object_detail', entry_info_dict),
(
Then, I upgraded the source code to work with Django v1.5. It raised errors when I's trying to access these generic date_based views above:
Could not import django.views.generic.date_based.archive_index. Parent module django.views.generic.date_based does not exist.
It is because in the newer version, Django 1.5, the generic function- based views are replaced by generic class-based views. So, I have to migrate the function-based views to class-based views:
urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from django.views.generic.dates import YearArchiveView
from django.views.generic.dates import MonthArchiveView
from django.views.generic.dates import DayArchiveView
from django.views.generic.dates import DateDetailView
from coltrane.models import Entry
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/?', include(admin.site.urls)),
url(r'search/$', 'cms.search.views.search'),
url(r'^weblog/$', ArchiveIndexView.as_view(model=Entry, date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/$', YearArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/$',
MonthArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
DayArchiveView.as_view(model=Entry,
date_field="pub_date")),
url(r'^weblog/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
DateDetailView.as_view(model=Entry,
date_field="pub_date")),
url(r'', include('django.contrib.flatpages.urls')),
)
References:
Django's documentation: https://docs.djangoproject.com/en/1.5/ref/class-based-views/generic-date-based/#module-django.views.generic.dates
Practical Django projects by James Bennett.
Comments
Post a Comment