将 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?
请您参考如下方法:
这是一个例子:
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
