全局名称'object_list'未在django 1.6上定义

时间:2014-01-28 21:57:50

标签: python django listview

将Django从1.4更新为1.6后,我遇到了global name 'object_list' is not defined问题

我有views.py:

def news(request, page):
    news = News.objects.filter(is_active=True).order_by('-timestamp')
    return object_list(
        request,
        page=page,
        queryset=news,
        paginate_by=3,
        template_object_name='news',
        template_name='blog-right-sidebar.html',
        extra_context={'current_page': 'news',
            'url_name': 'news', 'url_post_name': 'news_post'}

如何重写代码以使用ListView

1 个答案:

答案 0 :(得分:1)

以下是一个例子:

from django.views.generic import ListView


class NewsView(ListView):
    paginate_by = 3
    template_name = 'blog-right-sidebar.html',
    context_object_name = 'news'
    queryset = News.objects.filter(is_active=True).order_by('-timestamp')

    def get_context_data(self, **kwargs):
        # Here you can add any additional context items.
        context = super(NewsView, self).get_context_data(**kwargs)
        context['var'] = 'value'
        return context