I have my comments set up where users can review blog posts and have those reviews show up in the post_detail.html
template. I would like to display the total number of comments for every user, right next to their comments (in any post).
我设置了自己的评论,用户可以在其中查看博客帖子,并将这些评论显示在post_detail.html模板中。我想在其评论旁边(在任何帖子中)显示每个用户的评论总数。
For example, I'd like it to say user 1: 4 comments
, and so on, for each user. If user 1
has left 4 comments on 4 different posts, all 4 posts should display user 1: 4 comments
.
例如,我想为每个用户说出用户1:4评论,依此类推。如果用户1在4个不同的帖子上留下4条评论,则所有4个帖子都应显示用户1:4评论。
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
...
def user_rating_count(self):
user_ratings = Comment.objects.filter(user=2).count()
return user_ratings
...
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments")
user = models.ForeignKey(User, related_name="usernamee")
...
review_count = models.IntegerField(default=0)
views.py
@login_required
def add_comment(request, slug):
post = get_object_or_404(Post, slug=slug)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post # Makes the post linked to the comment
comment.user = request.user # Makes the user linked to the comment
comment.email = request.user.email
comment.picture = request.user.profile.profile_image_url()
comment.review_count = request.user.profile.user_rating_count()
comment.save()
return redirect('blog:post_detail', slug=post.slug)
else:
form = CommentForm()
template = "blog/post/add_comment.html"
context = {'form': form}
return render(request, template, context)
post_detail.html
...
{% for comment in post.comments.all %}
{{ comment.review_count }}
{% endfor %}
...
I've been trying to accomplish this through my model's user_rating_count
method, but I can only manually select user=1
or user=2
, which works, but it isn't dynamic. I'm trying to retrieve the user that left the comment.
我一直试图通过我的模型的user_rating_count方法来实现这一点,但我只能手动选择user = 1或user = 2,它可以工作,但它不是动态的。我正在尝试检索留下评论的用户。
1 个解决方案
#1
0
Try:
from django.db.models import Count
def user_rating_count(request):
users = User.objects.all().annotate(ratings=Count('comment'))
context={'users':users}
render(request, 'file.html', context)
Now in html
现在在HTML中
{% for user in users %}
<b>{{user.username}}</b>:{{user.ratings}}
{% endfor %}
This will show, username : total_comments
for every user.
这将显示每个用户的用户名:total_comments。
You need to make a call to user_rating_count
function from your urls
.
您需要从您的网址调用user_rating_count函数。
#1
0
Try:
from django.db.models import Count
def user_rating_count(request):
users = User.objects.all().annotate(ratings=Count('comment'))
context={'users':users}
render(request, 'file.html', context)
Now in html
现在在HTML中
{% for user in users %}
<b>{{user.username}}</b>:{{user.ratings}}
{% endfor %}
This will show, username : total_comments
for every user.
这将显示每个用户的用户名:total_comments。
You need to make a call to user_rating_count
function from your urls
.
您需要从您的网址调用user_rating_count函数。