Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

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

I have a blog page with a list of posts that are paginated with ListView and on the same page I have a newsletter form where a uses can enter an email an be subscribed and I tried this in order to achive all this on the same page but I get this error and I don't know how to get rid of it.

my view:

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

the error

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:UsersBularu LilianDesktopEnvironmentsecomonlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)
  File "C:UsersBularu LilianDesktopEnvironmentsecomonlibsite-packagesdjangocorehandlersase.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:UsersBularu LilianDesktopEnvironmentsecomonlibsite-packagesdjangoviewsgenericase.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:UsersBularu LilianDesktopEnvironmentsecomonlibsite-packagesdjangoviewsgenericase.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:UsersBularu LilianDesktopEcoMonlogviews.py", line 28, in post
    context = self.get_context_data(**kwargs)
  File "C:UsersBularu LilianDesktopEcoMonlogviews.py", line 44, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:UsersBularu LilianDesktopEnvironmentsecomonlibsite-packagesdjangoviewsgenericlist.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'
question from:https://stackoverflow.com/questions/65891607/bloglistview-object-has-no-attribute-object-list-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Under the hood ListView uses an attribute it sets called, object_list. This would normally be set in the get method, so you need to set it in your custom post method. The following should fix your error:

class BlogListView(ListView):
    ...

    def post(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        ... same as before

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...