基于django函数的通用视图迁移到基于类的视图

时间:2013-03-25 08:18:09

标签: django django-views

我最近从django 1.4迁移到django 1.5,我收到此错误

ViewDoesNotExist: Could not import django.views.generic.date_based.archive_index. Parent module django.views.generic.date_based does not exist.

谷歌搜索告诉我基于功能的视图已被弃用。我正在尝试迁移到基于类的视图,但发现它很难。这是我的views.py

from app.models import Season
from django.conf.urls import *
from cms.models import News, File


def get_extra_context():
    past_seasons = Season.objects.filter(in_progress = False)
    try:
        current_season = Season.objects.get(in_progress = True)
    except Season.DoesNotExist:
        current_season = None
    files = File.objects.all().order_by('-id')[:5]
    output = {
        'current_season': current_season,
        'past_seasons': past_seasons,
        'files': files,
        }
    return output

dictionary = {
    'queryset': News.objects.all(),
    'date_field': 'date',
    'extra_context': get_extra_context(),
}


urlpatterns= patterns(
    'django.views.generic.date_based',
    url(
        r'(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
        'object_detail',
        dict(dictionary, slug_field = 'slug', template_name = 'cms/news/object.html'),
        name='object'
    ),
    url(
        r'^$',
        'archive_index',
        dict(dictionary, template_name = 'cms/news/list.html'),
        name='list'
    )
)

我无法转换基于类的视图。可以任何身体帮助。 dictionary是我想要放入上下文的对象。

提前感谢。

//小鼠

1 个答案:

答案 0 :(得分:0)

这是我如何做到的。

urlpatterns = patterns(
    '',
    url(
        r'(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
        DateDetailView.as_view(
            model = News,
            date_field = 'date',
            slug_field = 'slug',
            template_name = 'cms/news/object.html',
            #dict = dictionary,
        ),
        name='object'
    ),
)