typeidea icon indicating copy to clipboard operation
typeidea copied to clipboard

'PostDetailView' object has no attribute 'object'

Open ahc2490416601 opened this issue 5 years ago • 0 comments

按第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'

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)`

`

ahc2490416601 avatar Sep 03 '20 16:09 ahc2490416601