繁体   English   中英

'BlogListView' object 在 Django 中没有属性 'object_list'

'BlogListView' object has no attribute 'object_list' in Django

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有一个博客页面,其中包含使用 ListView 分页的帖子列表,在同一页面上我有一个时事通讯表单,用户可以在其中输入 email 并进行订阅,我尝试这样做是为了在同一页面上实现所有这些但我得到这个错误,我不知道如何摆脱它。

我的观点:

class BlogListView(ListView):
    model = Post
    template_name = 'blog/blog.html'
    context_object_name = 'posts'
    ordering = ['-published_date']
    paginate_by = 2
    filterset_class = PostFilterForm
    post_search_title = None

    def post(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        email = request.POST.get('newsletter_email')
        if Newsletter.objects.filter(email=email).exists():
            messages.warning(request, messages_text['email_exists'])
        else:
            Newsletter.objects.create(email=email)
            messages.success(request, messages_text['email_subscribed'])
            return redirect('blog')
        return render(request, self.template_name, context)

    def get_queryset(self):
        queryset = super().get_queryset()
        self.post_search_title = self.filterset_class(self.request.GET, queryset=queryset)
        return self.post_search_title.qs.distinct()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['post_search_title'] = self.post_search_title
        tags = Post.tags.most_common()[:8]
        context['banner_page_title'] = template_titles['blog_title']
        context['page_location'] = template_titles['blog_path']
        context['tags'] = tags
        return context

错误

AttributeError at /blog/
'BlogListView' object has no attribute 'object_list'

Environment:


Request Method: POST
Request URL: http://localhost:8000/blog/

Django Version: 3.1.4
Python Version: 3.9.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'home.apps.HomeConfig',
 'blog.apps.BlogConfig',
 'users.apps.UsersConfig',
 'taggit',
 'widget_tweaks',
 'django_filters',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.google',
 'allauth.socialaccount.providers.facebook']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Bularu Lilian\Desktop\Environments\ecomon\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Bularu Lilian\Desktop\Environments\ecomon\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Bularu Lilian\Desktop\Environments\ecomon\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Bularu Lilian\Desktop\Environments\ecomon\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Bularu Lilian\Desktop\EcoMon\blog\views.py", line 28, in post
    context = self.get_context_data(**kwargs)
  File "C:\Users\Bularu Lilian\Desktop\EcoMon\blog\views.py", line 44, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\Bularu Lilian\Desktop\Environments\ecomon\lib\site-packages\django\views\generic\list.py", line 115, in get_context_data
    queryset = object_list if object_list is not None else self.object_list

Exception Type: AttributeError at /blog/
Exception Value: 'BlogListView' object has no attribute 'object_list'
1 个回复

在引擎盖下ListView使用它设置的一个名为object_list的属性。 这通常会在get方法中设置,因此您需要在自定义post方法中进行设置。 以下应该可以解决您的错误:

class BlogListView(ListView):
    ...

    def post(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        ... same as before
5 'ProductList'对象没有属性'object_list'

在我的ProductList类中,当我尝试在另一个方法中调用get_context_data时,我收到错误'ProductList' object has no attribute 'object_list' 是什么导致了这个错误? 如何在第二种方法中获取object_list ? ...

6 'UserDetailView' object 没有属性 'object_list'

大家,在用户详细信息页面中,我有用户的帖子。 我想分页(计划稍后进行无限分页),试图做到这一点。 我收到错误'UserDetailView' object has no attribute 'object_list',我一直在寻找解决方案。 但没有找到明确的示例如何在详细视图中对相关对象进行分页。 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM