Open ahc2490416601 opened 4 years ago
按第9章完善完 PostDetailView后就报这个错'PostDetailView' object has no attribute 'object' 问题出在哪里,希望老师指点
'PostDetailView' object has no attribute 'object'
`class PostDetailView(CommonViewMixin, DetailView): queryset = Post.latest_posts() template_name = 'blog/detail.html' context_object_name = 'post' pk_url_kwarg = 'post_id'
def get(self, request, *args, **kwargs): response = super().get(request, *args, **kwargs) self.handle_visited() return response def handle_visited(self): increase_pv = False increase_uv = False uid = self.request.uid pv_key = 'pv:%s:%s' % (uid, self.request.path) if not cache.get(pv_key): increase_pv = True cache.set(pv_key, 1, 1 * 60) # 1分钟有效 uv_key = 'uv:%s:%s:%s' % (uid, str(date.today()), self.request.path) if not cache.get(uv_key): increase_uv = True cache.set(uv_key, 1, 24 * 60 * 60) # 24小时有效 if increase_pv and increase_uv: Post.objects.filter(pk=self.object.id).update(pv=F('pv') + 1, uv=F('uv') + 1) elif increase_pv: Post.objects.filter(pk=self.object.id).update(pv=F('pv') + 1) elif increase_uv: Post.objects.filter(pk=self.object.id).update(uv=F('uv') + 1)`
`
按第9章完善完 PostDetailView后就报这个错
'PostDetailView' object has no attribute 'object'
问题出在哪里,希望老师指点`class PostDetailView(CommonViewMixin, DetailView): queryset = Post.latest_posts() template_name = 'blog/detail.html' context_object_name = 'post' pk_url_kwarg = 'post_id'
`