gpt4 book ai didi

python - 全局名称 'object_list' 未在 Django 1.6 上定义

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:01 24 4
gpt4 key购买 nike

将 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

关于python - 全局名称 'object_list' 未在 Django 1.6 上定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com