0

I have a project that may give points to persons who post.

I have two models (Post and Points).

I want to count each user post in (Post) and multiply the result to create_post_points field in (Points) and show the result in template.

What i tried to do (signals, aggregation, queryset, intermediate class in models) but i failed.

Can any one help me?

Models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    likes = models.ManyToManyField(User, related_name='likes', blank=True)
    post_date = models.DateTimeField(default=timezone.now)
    post_update = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    favourite = models.ManyToManyField(User, related_name='favourite', blank=True)

class Points (models.Model):
        create_post_points = models.IntegerField()
        create_comment_points = models.IntegerField()

Views.py

def PostListView(request):
posts = Post.objects.all()
context = {
    'title': 'Home Page',
    'posts': posts,
}
return render(request, 'blog/index.html', context)

Templates

{% for post in posts %}
<div class="border p-3 mb-3">
    <div class="media">
    <img class="img-fluid rounded-circle border m-2" style="width: 70px; height:70px;"
        src="{{post.author.profile.image.url}}" alt="User Photo">
      <div class="media-body">
        <h5><a class="text-primary" href="{% url 'detail' post.id %}">{{post.title}}</a></h5>
          <h6 class="text-dark">Auther Score : {{******}}</h6>
        <p>{{post.content |truncatechars:150 }}
        <a class="text-primary" href="{% url 'detail' post.id %}">read more</a></p>
      </div>
    </div>
</div>
{% endfor %}
Mehdi
  • 717
  • 1
  • 7
  • 21
Mohamed Abbase
  • 111
  • 3
  • 15

1 Answers1

0

In your views.py you are rendering all of your posts. Instead of that, i think you probably need to render the count of all the posts by users. Getting a count of objects in a queryset in django Check this link out too. This might help.

VeryBhatti
  • 119
  • 7
  • Thank you for your replay, but i tried answers in this page, but it did't solved my problem. can you please write any sample to study it. i appreciate your kind help. – Mohamed Abbase Feb 29 '20 at 15:45