Home > OS >  I want create comment section that can only logged in users can use but I have this problem
I want create comment section that can only logged in users can use but I have this problem

Time:11-18

I get an Error:

cannot unpack non-iterable bool object

  profile = Profile.objects.get(Profile.user == request.user)

This is my models.py in account app and blog app:

class Profile(models.Model):
    STATUS_CHOICES = (
        ('manager', 'مدیر'),
        ('developer', 'توسعه‌دهنده'),
        ('designer', 'طراح پروژه'),
    )
    
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    bio  = models.CharField(max_length=50, blank=True)
    task = models.CharField(choices=STATUS_CHOICES, max_length=20, blank=True, null=True, default=None)
    date_of_birth = models.DateField(blank=True, null=True)
    photo = models.ImageField(upload_to='users/photos/%Y/%m/%d/', blank=True)
    
    def __str__(self):
        return f'{self.user.get_full_name()}'

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)

and this is my views.py for comments:

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(Profile.user == request.user)
    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 redirect('post_detail', slug=post.slug)
    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})

Is there any solution for this problem?

CodePudding user response:

Assuming you need to get only single profile instance i.e. current logged in user's profile so you can either use:

 profile = Profile.objects.get(user=request.user)

or:

get_object_or_404(Profile, user=request.user)

To limit the view to be accessed by only logged in users, use @login_required decorator so:

@login_required(login_url='/accounts/login/') # you can give any login_url you want.
def post_detail(request, year, month, day, slug):
    #...
  • Related