【问题标题】:django function based generic views migration to class based viewsdjango 基于函数的通用视图迁移到基于类的视图
【发布时间】:2013-03-25 08:18:08
【问题描述】:

我最近从 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.

谷歌搜索让我发现基于功能的视图已被贬低。我正在尝试迁移到基于类的视图,但发现它很困难。这是我的意见。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'
    )
)

我无法将其转换为基于类的视图。任何身体都可以提供帮助。字典是我想要放入上下文的对象。

提前致谢。

//鼠标

【问题讨论】:

    标签: django django-views


    【解决方案1】:

    这就是我的做法。

    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'
        ),
    )
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2021-03-28
      • 2013-01-25
      • 2021-05-30
      • 1970-01-01
      相关资源
      最近更新 更多