ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-类型对象“ Post”没有属性“ published” Django

2019-10-25 19:56:33  阅读:145  来源: 互联网

标签:python django tags


我正在开发一个应用程序,并且试图显示基于标签的相关文章.我一切正常,但是当我在浏览器中加载详细信息视图时,出现错误,提示类型对象“ Post”没有属性“ published”,我已在下面发布了我的代码.

模型:

class Post(models.Model):
    """docstring for Post."""
    STATUS_CHOICES = (
        ('drafts', 'Draft'),
        ('published', 'Published'), )
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) #blank=True, null=True)#default=1
    title = models.CharField(max_length = 120)
    slug = models.SlugField(unique= True)
    draft = models.BooleanField(default = False)
    publish = models.DateField(auto_now=False, auto_now_add=False)
    content = models.TextField()
    tags = TaggableManager()
    status = models.CharField(max_length=10,choices=STATUS_CHOICES, default='published')
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

视图:

def view(request, slug =None):
    instance = get_object_or_404(Post, slug =slug)
    if instance.draft or instance.publish > timezone.now().date():
        redirect(index)
    #content_type = ContentType.objects.get_for_model(Post)
    #obj_id = instance.id
    initial_data = {
            "content_type": instance.get_content_type,
            "object_id": instance.id
    }
    form = CommentForm(request.POST or None, initial=initial_data)
    if form.is_valid():
        #print  (form.cleaned_data)
        c_type = form.cleaned_data.get("content_type")
        content_type = ContentType.objects.get(model= c_type)
        obj_id = form.cleaned_data.get("object_id")
        c_content =form.cleaned_data.get("content")
        parent_obj = None
        try:
            parent_id = int(request.POST.get("parent_id"))
        except Exception as e:
            parent_id = None

        if parent_id:
            parent_query = Comment.objects.filter(parent__id= parent_id)
            if parent_query.exists():
                parent_obj = parent_query.first()

        new_comment, created = Comment.objects.get_or_create(
                user = request.user,
                content_type = content_type,
                object_id = obj_id,
                content = c_content,
                parent = parent_obj,

                )
        return HttpResponseRedirect(new_comment.content_object.get_absolute_url())


    comments = instance.comments
    # List of similar posts
    post_tags_ids = instance.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=instance.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]
    context = {
    #"objects": query,
    "instance": instance,
    "comments": comments,
    "form": form,
    'similar_posts': similar_posts
    }
    template = 'view.html'
    return render(request,template,context)

附加代码将应要求添加.提前致谢.

解决方法:

如果我理解正确,那么您有一个印刷错误

similar_posts = Post.published.filter(tags__in=post_tags_ids)
                                     .exclude(id=instance.id)

上面的行应该是

similar_posts = Post.objects.filter(tags__in=post_tags_ids)
                                     .exclude(id=instance.id)

另外,如果您打算使用字段发布,那么它只能在queryset args中使用,而不能用作“相关对象”属性

标签:python,django,tags
来源: https://codeday.me/bug/20191025/1931152.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有