いつもお世話になります。
現在Djangoを使用して口コミサイトを開発しており、詰まったので質問させていただきます。
実現したいこと
- 各カテゴリーのページを作成したい
- 上記のカテゴリーページからカテゴリーのページに遷移し、そのカテゴリーに投稿された口コミを表示させたい
画面遷移の流れ
- トップページにアクセス
- カテゴリーボタンをクリック
- カテゴリ一覧に遷移
- カテゴリーをクリック(仮に「商品A」にします)
- 商品Aの詳細ページに遷移
- 商品Aカテゴリーに投稿された口コミ一覧を表示
投稿までの流れ
- トップページより投稿ボタンのクリック
- フォームから口コミ投稿するカテゴリーを選択し、投稿
- 紐づくカテゴリーページの一覧に表示
現状
カテゴリーの詳細ページまで作成したのですが、カテゴリー詳細ページにユーザーが投稿した口コミを表示させる方法が分からない状況です
model.py
from django.db import models from django.utils import timezone # カテゴリーモデル class category(models.Model): class Meta: verbose_name = 'カテゴリー' # カラムの定義 category_name = models.CharField(max_length=255, db_column='site_name', verbose_name='カテゴリー') def __str__(self): return self.category_name # 投稿内容モデル class contents(models.Model): class Meta: verbose_name = '投稿内容' # カラムの定義 post_name = models.CharField(max_length=20, default='匿名', blank=False, null=False) category_name = models.ForeignKey(category, on_delete=models.CASCADE) price_score = models.CharField(max_length=10, blank=True, null=True) satisfaction_score = models.CharField(max_length=10, blank=True, null=True) course_score = models.CharField(max_length=10, blank=True, null=True) understand_score = models.CharField(max_length=10, blank=True, null=True) support_score = models.CharField(max_length=10, blank=True, null=True) good_point_contents = models.TextField(blank=True, null=True) bad_point_contents = models.TextField(blank=True, null=True) overall = models.TextField(blank=True, null=True) insert_ymd = models.DateTimeField(default=timezone.now) def __str__(self): return self.post_name def split_contens(self): return self.post_contents[:30]
urls.py
from django.urls import path from .views import * # app_nameはアプリケーション名 app_name = 'app' urlpatterns = [ path('', index_view.as_view(), name='index'), path('category/', category_view.as_view(), name='category'), path('category/<int:pk>', category_detail_view.as_view(), name='category-detail'), path('post/', create_view.as_view(), name='create'), ]
forms.py
from django import forms from .models import contents CHOICE = [ ('1', 'かなり不満'), ('2', 'やや不満'), ('3', '普通'), ('4', 'やや満足'), ('5', 'かなり満足'), ] class post_form(forms.ModelForm): post_name = forms.CharField(label='投稿者名', widget=forms.TextInput()) price_score = forms.ChoiceField(label='料金', widget=forms.Select, choices=CHOICE, initial=2) satisfaction_score = forms.ChoiceField(label='満足度', widget=forms.Select, choices=CHOICE, initial=2) course_score = forms.ChoiceField(label='コース', widget=forms.Select, choices=CHOICE, initial=2) understand_score = forms.ChoiceField(label='分かりやすさ', widget=forms.Select, choices=CHOICE, initial=2) support_score = forms.ChoiceField(label='サポート', widget=forms.Select, choices=CHOICE, initial=2) good_point_contents = forms.CharField(label='良かった点', widget=forms.Textarea) bad_point_contents = forms.CharField(label='改善してほしい点', widget=forms.Textarea) overall = forms.CharField(label='総合評価', widget=forms.Textarea) class Meta: model = contents fields = ('post_name', 'category_name', 'price_score', 'satisfaction_score', 'course_score', 'understand_score', 'support_score', 'good_point_contents', 'bad_point_contents', 'overall')
views.py
from django.shortcuts import render from django.views import generic from .forms import post_form from .models import contents, category from django.urls import reverse_lazy # Create your views here. class index_view(generic.TemplateView): template_name = 'app/index.html' class create_view(generic.CreateView): template_name = 'app/post-study-site.html' model = contents form_class = Study_site_post_form success_url = reverse_lazy('app:index') class category_view(generic.ListView): model = category template_name = 'app/category.html' class category_detail_view(generic.DetailView): model = category template_name = 'app/category-detail.html'
category.html
{% extends 'base.html' %} {% load widget_tweaks %} {% block title %}トップ{% endblock %} {% block contents %} <div> カテゴリー {% for list in object_list %} <a href="{% url 'app:category-detail' list.pk %}">{{ list.category_name }}</a> {% endfor %} </div> {% endblock %}
category-detail.html
{% extends 'base.html' %} {% load widget_tweaks %} {% block title %}トップ{% endblock %} {% block contents %} <div> カテゴリー詳細 </div> {% endblock %}
イメージ図
あなたの回答
tips
プレビュー