姜戈属性错误:"unicode object has no attribute tags"



一切都运行良好,所以我无论如何都假设,但是当我运行服务器并单击我博客的标题时,我得到一个 AttributeError 说"unicode 对象没有属性标签">

这是处理标签的代码段

def post_detail(request, year, month, day, post):
     post_tags_ids = post.tags.values_list('id', flat=True)
     similar_posts =  Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)                     
     similar_posts =  similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]
     post = get_object_or_404(Post, slug=post,status='published',publish__year=year,publish__month=month,publish__day=day)
     return render(request,'blog/post/detail.html',{'post': post,'comments': comments,'comment_form': comment_form,'similar_posts': similar_posts})

您传递的"post"参数似乎不是您假设的模型实例。我认为您需要先获取帖子对象。假设您将 pk 作为帖子传递:

post_object = get_object_or_404(Post, pk=post)
post_tags_ids = post_object.tags.values_list('id', flat=True)

相关内容

最新更新