ContentDetailViewからIndexViewへのコメントに関するカット機能の後、私のサーバーを実行できず、動作しません。変更クラスの機能が動作しない
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)
エラー:
'IndexView' object has no attribute 'object'
誰かが私を助けることができますか?
関数がContentDetailViewクラスにあったときに機能します。 ContentDetailViewクラスはtemplate_nameとcontext_object_nameという同じモデルを持ちます。
まず、「うまくいきません」というのは参考にはなりません。[ask]をご覧ください。今、 'ListView'の"オブジェクト "は何でしょうか?それは 'DetailView'だったときのオブジェクトは何でしたか? – Sayse
関数def get_context_dataとdef_postは、ContentDetailViewクラスにありました。今私はIndexViewクラスにその関数をスローする必要があります。最後には動作しません。私はcontext_object_name = 'オブジェクト'を貼り付けたときにそのエラーが発生し、初心者で何ができるのかわかりません。能力と英語の不足のために申し訳ありません。 – Dinson