Я хочу, чтобы только пользователи, у которых нет неактивных комментариев, могли публиковать новые комментарии

Comment matching query does not exist.
inactive_comments = Comment.objects.get(active=False, profile=profile)

Вот мое мнение:

def post_detail(request, year, month, day, slug):
    post = get_object_or_404(Post, slug=slug, status='published', publish__year=year, publish__month=month, publish__day=day)
    tags = Tag.objects.all()
    tagsList = []
    for tag in post.tags.get_queryset():
        tagsList.append(tag.name)
    profile = Profile.objects.get(user=request.user)
    inactive_comments = Comment.objects.get(active=False, profile=profile)
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.profile = profile
            new_comment.post = post
            new_comment.save()
            
            return HttpResponseRedirect(request.path_info)
    else:
        comment_form = CommentForm()
        
    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')[:3]
        
    return render(request, 'blog/post/detail.html', {'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form, 'similar_posts': similar_posts, 'tagsList': tagsList, 'tags': tags, 'inactive_comments': inactive_comments})

Это мои модели комментариев:

class Comment(models.Model):
    post                = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    profile             = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='user_comments')
    body                = models.TextField()
    created             = models.DateTimeField(auto_now_add=True)
    updated             = models.DateTimeField(auto_now=True)
    active              = models.BooleanField(default=False)

и это мой шаблон:

                         {% if request.user.is_authenticated %}
                         <div class="new_comment">
                              {% if inactive_comments %}
                                   <h4>نظر شما با موفقیت ثبت شد و در حال بررسی است.</h4>                                                             
                              {% else %}     
                              <h4>نظر خود را اضافه کنید</h4></br>
                              <form method="post">                               
                                   <div class="row" dir="rtl">
                                        <div class="col-sm-12 col-md-8">
                                             {{ comment_form.body|attr:"class:form-control"|attr:"type:text" }}
                                        </div>
                                   </div>
                                   {% csrf_token %} 
                                   <div class="row"><br/>
                                        <div class="col-sm-12 col-md-8"> <input type="submit" value="ارسال نظر" class="btn send btn-primary" href="#"></input> </div>
                                   </div>                                  
                              </form>          
                              {% endif %}
                         </div>
                         {% else %}
                         <p>برای انتشار دیدگاه خود <a href="{% url 'account:login' %}">وارد پروفایل کاربری</a> خود شوید یا در سایت <a href="{% url 'account:register' %}">ثبت نام</a> کنید.</p>
                         {% endif %}

Я решил проблему, заменив этот код:

inactive_comments = post.comments.filter(active=False, profile=profile)
Вернуться на верх