2017-08-27 56 views
0

私は自分の投稿にコメント/評価を残すことができるブログを作っています。その特定の投稿のすべての評価の平均を表示したいと思います。それ、どうやったら出来るの?django - テンプレート内の平均を表示する

models.py

class Post(models.Model): 
    STATUS_CHOISES = (

     ('draft', 'Draft'), 
     ('published', 'Published'), 

     ) 
    category = models.ForeignKey(Category) 
    title = models.CharField(max_length=250) 
    slug = models.SlugField(max_length=250, unique=True) 
    content = models.TextField() 
    seo_title = models.CharField(max_length=250) 
    seo_description = models.CharField(max_length=160) 
    author = models.ForeignKey(User, related_name='blog_posts', default=settings.AUTH_USER_MODEL) 
    published = models.DateTimeField(default=timezone.now) 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 
    status = models.CharField(max_length=9, choices=STATUS_CHOISES, default='draft') 

    def get_absolute_url(self): 
     return reverse('blog:post_detail', args=[self.slug]) 


    def __str__(self): 
     return self.title 



class Comment(models.Model): 
    difficulty_rating_choices = (

     (1, 'Very Easy'), 
     (2, 'Easy'), 
     (3, 'Moderate'), 
     (4, 'Hard'), 
     (5, 'Very Hard'), 

    ) 

    workload_rating_choices = (

     (1, 'Very Light'), 
     (2, 'Light'), 
     (3, 'Moderate'), 
     (4, 'Heavy'), 
     (5, 'Very Heavy'), 

    ) 

    book_rating_choices = (

     (1, '$'), 
     (2, '$$'), 
     (3, '$$$'), 
     (4, '$$$$'), 
     (5, '$$$$$'), 

    ) 

    attendance_rating_choices = (

     (1, 'Not Required'), 
     (2, 'Required'), 


    ) 
    post = models.ForeignKey(Post, related_name="comments") 
    user = models.CharField(max_length=250) 
    email = models.EmailField() 
    title = models.CharField(max_length=250) 
    body = models.TextField() 
    created = models.DateTimeField(auto_now_add=True) 
    approved = models.BooleanField(default=False) 
    difficulty_rating = models.IntegerField(choices=difficulty_rating_choices) 
    workload_rating = models.IntegerField(choices=workload_rating_choices) 
    book_rating = models.IntegerField(choices=book_rating_choices) 
    attendance_rating = models.IntegerField(choices=attendance_rating_choices) 


    def approved(self): 
     self.approved = True 
     self.save() 

    def __str__(self): 
     return self.title 

views.py

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 
      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) 

私は平均が私の4つの基準(difficulty_rating、workload_rating、book_ratingごとに表示するテンプレート

<h1>Difficulty: <h1> 
     <h1>Workload: <h1> 
     <h1>Book Cost: <h1> 
     <h1>Attendance: <h1> 

     <hr> 
     <h2>{{ post.title }}</h2> 
     <p>Written by {{ post.author }} on {{ post.published }}</p> 
     <hr> 
     {{ post.content|linebreaks }} 
     <hr> 
     <h2>Comments</h2> 
     <p>Total number of comments: {{ post.comments.count }}</p> 
     <a href="{% url 'blog:add_comment' slug=post.slug %}">Leave a comment</a> 
     {% for comment in post.comments.all %} 


      <p>Title: {{ comment.title }}</p> 
      <p>Review: {{ comment.body }}</p> 
      <p>Date: {{ comment.created }}</p> 
      <p>Posted by: {{ comment.user }}</p> 
      <p>Difficulty: {{ comment.get_difficulty_rating_display }}</p> 
      <p>Workload: {{ comment.get_workload_rating_display }}</p> 
      <p>Book Cost: {{ comment.get_book_rating_display }}</p> 
      <p>Attencance: {{ comment.get_attendance_rating_display }}</p> 
      <hr> 

     {% empty %} 
      <p>There are no comments yet. Be the first one to comment!</p> 

     {% endfor %} 

、 attendance_rating)をテンプレートの上部に表示します。それをどうやってやりますか?これまでのところ私はそれを理解することができませんでした..

これはテンプレートレベルで行われますか?私はviews.pyを編集する必要がありますか?それともモデルそのものなのでしょうか?

+0

ビューで平均を計算し、そしてように、テンプレートの中でそれを渡します別個の商品。 –

答えて

1

あなたのPostモデル内のヘルパーメソッドを書くことができます:

from django.db.models import Avg 

class Post(models.Model): 
    # ... 
    def avg_ratings(self): 
     return self.comments.aggregate(
      Avg('difficulty_rating'), 
      Avg('workload_rating'), 
      Avg('book_rating'), 
      Avg('attendance_rating'), 
     ) 

次にあなたができるテンプレートで:

{% with avg_ratings=post.avg_ratings %} 
    {{ avg_ratings.difficulty_rating__avg }} 
    {{ avg_ratings.workload_rating__avg }} 
    {{ avg_ratings.book_rating__avg }} 
    {{ avg_ratings.attendance_rating__avg }} 
{% endwith %} 
+0

はうまくいった!ありがとうございました!代わりに人間が読むことができる相手を表示したい場合はどうすればよいですか?私の選択では、値が3のときに人間が読むことができる「$$$」というbook_ratingがあります。これを行う方法はありますか?私の推測では、それを最も近い整数に丸める必要がありますか? – dmandres

+0

問題ありません!あなたが立ち往生している場合は、別の質問を作成してください。しかし、はい、あなたはそれを最も近い整数に丸めてそこから行く必要があります。しかし、avgが2.5になった場合、それを3または2と見なすのはどうでしょうか?それはあなたが決定する必要があります。 –

関連する問題