お世話になります。
Django(2.0)で掲示板アプリを作っている所なのですが
仮で作ってみたものが以下の通りです。
書き込み順は123の順ですが
返信が対象コメントの下に来るようにしているため、上から231の順になっています。
このような掲示板ならできるのですが、これを以下のように
「通常の書き込みも返信も区別なく上から順番に表示(321)」としたいです。
ただ、返信には「~さんへの返信」等の情報を付加して表示したいため、modelを分けています。
通常の書き込みと返信とを混合して順に表示する方法が全く思いつきません。
アイデアをくださる方、いらっしゃいませんか?
よろしくお願いいたします。
(models.py)
Python
1from django.db import models 2 3MARK_CHOICES = ( 4 ('質問です', '質問です'), 5 ('答えます', '答えます'), 6 ('ありがとう', 'ありがとう'), 7 ('重要', '重要'), 8 ('宣言', '宣言'), 9 ('報告', '報告'), 10) 11 12EVALUATION_CHOICES = ( 13 ('できた', 'できた'), 14 ('まあまあ', 'まあまあ'), 15 ('あんまり', 'あんまり'), 16) 17 18 19class Comment(models.Model): 20 id = models.AutoField(primary_key=True) 21 user = models.CharField(max_length=20) 22 title = models.CharField(max_length=100) 23 text = models.TextField() 24 mark = models.CharField(max_length=5, blank=True, choices=MARK_CHOICES) 25 evaluation = models.CharField(max_length=4, blank=True, choices=EVALUATION_CHOICES) 26 created_at = models.DateTimeField(auto_now_add=True) 27 updated_at = models.DateTimeField(auto_now=True) 28 29 class Meta: # (1) 30 db_table = 'comments' 31 ordering = ['-created_at'] 32 33 def __str__(self): # (2) 34 return self.user + '_' + self.title 35 36 37class Reply(models.Model): 38 id = models.AutoField(primary_key=True) 39 user = models.CharField(max_length=20) 40 title = models.CharField(max_length=100) 41 text = models.TextField() 42 mark = models.CharField(max_length=5, blank=True, choices=MARK_CHOICES) 43 evaluation = models.CharField(max_length=4, blank=True, choices=EVALUATION_CHOICES) 44 target = models.ForeignKey(Comment, on_delete=models.CASCADE) 45 created_at = models.DateTimeField(auto_now_add=True) 46 updated_at = models.DateTimeField(auto_now=True) 47 48 class Meta: 49 db_table = 'replies' 50 ordering = ['-created_at'] 51 52 def __str__(self): 53 return self.user + '_' + self.title
(views.py)
Python
1from django.views.generic import ListView, DetailView, CreateView, UpdateView,DeleteView 2from django.urls import reverse_lazy 3from django.shortcuts import redirect 4from .models import Comment, Reply 5 6 7class CommentListView(ListView): 8 model = Comment 9 10 def get_queryset(self): 11 queryset = Comment.objects.all().prefetch_related('reply_set').order_by('-created_at') 12 return queryset 13 14 15class CommentDetailView(DetailView): 16 model = Comment 17 18 def get_context_data(self, **kwargs): 19 context = super().get_context_data(**kwargs) 20 comment_pk = self.kwargs['pk'] 21 # print(comment_pk) 22 reply_list = Reply.objects.filter(target_id=comment_pk) 23 context['reply_list'] = reply_list 24 return context 25 26 27class CommentCreateView(CreateView): 28 model = Comment 29 fields =['user', 'title', 'text', 'mark', 'evaluation'] 30 template_name = 'thread/comment_form.html' 31 success_url = reverse_lazy('comment_list') 32 33 34class CommentUpdateView(UpdateView): 35 model = Comment 36 fields = ['user', 'title', 'text', 'mark', 'evaluation'] 37 success_url = reverse_lazy('comment_list') 38 39 40class CommentDeleteView(DeleteView): 41 model = Comment 42 success_url = reverse_lazy('comment_list') 43 44 45class ReplyCreateView(CreateView): 46 model = Reply 47 fields = ['user', 'title', 'text', 'mark', 'evaluation'] 48 template_name = 'thread/reply_form.html' 49 success_url = reverse_lazy('comment_list') 50 51 def get_context_data(self, **kwargs): 52 context = super().get_context_data(**kwargs) 53 comment_pk = self.kwargs['pk'] 54 context['id'] = comment_pk 55 context['name'] = Comment.objects.get(pk=comment_pk).user 56 return context 57 58 def form_valid(self, form): 59 comment_pk = self.kwargs['pk'] 60 comment = Comment.objects.get(pk=comment_pk) 61 reply = form.save(commit=False) 62 reply.target = comment 63 reply.save() 64 return redirect('comment_list')
(comment_list.html)
HTML
1{% extends 'thread/base.html' %} 2 3{% block content %} 4 {% for comment in comment_list %} 5 <h4>コメント</h4> 6 <p> 7 <a href="{% url 'comment_detail' comment.id %}">#{{ comment.id }}</a> 8 {{ comment.title }} 9 </p> 10 <p>created_at: {{ comment.created_at }}</p> 11 <p>mark: {{ comment.mark }}</p> 12 <p>user: {{ comment.user }}</p> 13 <p>text: {{ comment.text | linebreaksbr }}</p> 14 <a href="{% url 'comment_reply' comment.id %}">返信</a> 15 <a href="{% url 'comment_update' comment.id %}">編集</a> 16 <a href="{% url 'comment_delete' comment.id %}">削除</a> 17 </p> 18 <hr> 19 {% for reply in comment.reply_set.all %} 20 <h4>返信</h4> 21 <p>#{{ reply.id }}</p> 22 <p>title: {{ reply.title }}</p> 23 <p>user: {{ reply.user }}</p> 24 <p>mark: {{ reply.mark }}</p> 25 <p>text: {{ reply.text }}</p> 26 <hr> 27 {% endfor %} 28 {% endfor %} 29{% endblock %}
あなたの回答
tips
プレビュー