简体   繁体   中英

After change class function doesn't work

After cut function about comments from ContentDetailView to IndexView I can't run my server and it doesn't work.

class IndexView(AllAdsViewMixin, ListView):
model = UserContent
template_name = 'user_content/list.html'
context_object_name = 'usercontent'

def get_queryset(self):
    """Return the last all five published contents"""
    return UserContent.objects.filter(state='1').order_by('-published')[:5]

def get_context_data(self, **kwargs):
    content_type = self.object.get_content_type
    initial_data = {
        'content_type': content_type,
        'object_id': self.object.id,
    }

    comment_form = CommentForm(initial=initial_data)
    comments = Comment.objects.filter(
        content_type=content_type,
        object_id=self.object.id
    )

    context = super(IndexView, self).get_context_data(**kwargs)
    context['comments'] = comments
    context['comment_form'] = comment_form

    return context

def post(self, request, **kwargs):
    self.object = self.get_object()
    content_type = self.object.get_content_type
    comment_form = CommentForm(request.POST)

    if comment_form.is_valid():
        content_data = comment_form.cleaned_data.get('content')
        parent_obj = None
    try:
        parent_id = int(request.POST.get('parent_id'))
    except:
        parent_id = None

    if parent_id:
        parent_qs = Comment.objects.filter(parent__id=parent_id)
        if parent_qs.exists() and parent_qs.count() == 1:
            parent_obj = parent_qs.first()

    new_comment, created = Comment.objects.get_or_create(
        user=request.user,
        content_type=content_type,
        object_id=self.object.id,
        content=content_data,
        parent=parent_obj
    )

    return self.get(request, **kwargs)

Error:

'IndexView' object has no attribute 'object'

Can someone help me?

When function was in class ContentDetailView it works. ContentDetailView class has the same model, template_name and context_object_name.

Your ContentDetailView class was inherited from DetailView , but your IndexView is inherited from ListView .

If you look at docs of List View you can see that there is no method named get_object() . As in the docs,

The List View render some list of objects, set by self.model or self.queryset . self.queryset can actually be any iterable of items, not just a queryset.

So, the self.object you refer to in the view, doesn't exist.

But in the case of Detail View , if you look at the docs,

The DetailView render a "detail" view of an object.

By default this is a model instance looked up from self.queryset , but the view will support display of any object by overriding self.get_object() .

The logic which you are implementing in your IndexView is clearly wrong. You would need to look into the docs first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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