djangoでforを使って商品を全て表示させたいのですが、表示できません。
forを記述しなければ当たり前ですが、一件だけ表示させることはできます。(forを書くと一つも表示されない)
detail.htmlが今回の質問したい箇所ですが、index.htmlでは店舗一覧をforを使って全て表示することができています。
制作途中で少し見にくいかもしれませんが、よろしくお願いします。
base.html
html
1{% load static %} 2 3<!DOCTYPE html> 4<html lang="ja"> 5 <head> 6 <meta charset="utf-8" /> 7 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> 8 <meta name="description" content="" /> 9 <meta name="author" content="" /> 10 <title>Small Business - Start Bootstrap Template</title> 11 <!-- Favicon--> 12 <link rel="icon" type="image/x-icon" href="assets/favicon.ico" /> 13 <!-- Core theme CSS (includes Bootstrap)--> 14 <link href="{% static 'css/styles.css' %}" rel="stylesheet" /> 15 {% block head %}{% endblock %} 16 </head> 17 18 <body> 19 <!--ヘッダーの追加--> 20 <!--ナビゲーションバー--> 21 <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> 22 <div class="container px-5"> 23 <a class="navbar-brand" href="{% url 'shoplist:index' %}">kohriInfo</a> 24 <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button> 25 <div class="collapse navbar-collapse" id="navbarSupportedContent"> 26 <ul class="navbar-nav ms-auto mb-2 mb-lg-0"> 27 <li class="nav-item"><a class="nav-link active" aria-current="page" href="{% url 'shoplist:index' %}">shopInfo</a></li> 28 <li class="nav-item"><a class="nav-link" href="#!">community</a></li> 29 <li class="nav-item"><a class="nav-link" href="#!">Contact</a></li> 30 <li class="nav-item"><a class="nav-link" href="#!">Contact</a></li> 31 </ul> 32 </div> 33 </div> 34 </nav> 35 <header> 36 {% block header %}{% endblock %} 37 </header> 38 39 <!--メインの追加--> 40 <main class="container px-4 px-lg-5"> 41 {% block main %}{% endblock %} 42 </main> 43 44 <!--フッターの追加--> 45 <footer class="py-5 bg-dark"> 46 <div class="container px-4 px-lg-5"><p class="m-0 text-center text-white">Copyright © Your Website 2021</p></div> 47 {% block footer %} 48 {% endblock %} 49 </footer> 50 <!-- Bootstrap core JS--> 51 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"></script> 52 <!-- Core theme JS--> 53 <script src="{% static 'js/scripts.js' %}"></script> 54 </body> 55</html>
index.html
html
1{% extends "base.html" %} 2{% load static %} 3 {% block header %} 4 <!-- Responsive navbar--> 5 <!-- Page Content--> 6 <div class="container px-4 px-lg-5"> 7 <!-- Heading Row--> 8 <div class="row gx-4 gx-lg-5 align-items-center my-5"> 9 <div class="col-lg-7"><img class="img-fluid rounded mb-4 mb-lg-0" src="{% static 'image/けやき通り.jpg' %}" alt="..." width="990" height="400" /></div> 10 <div class="col-lg-5"> 11 <h1 class="font-weight-light">Business Name or Tagline</h1> 12 <p>This is a template that is great for small businesses. It doesn't have too much fancy flare to it, but it makes a great use of the standard Bootstrap core components. Feel free to use this template for any project you want!</p> 13 <a class="btn btn-primary" href="#!">Call to Action!</a> 14 </div> 15 </div> 16 <!-- Call to Action--> 17 <div class="card text-white bg-secondary my-5 py-4 text-center"> 18 <div class="card-body"><p class="text-white m-0">〜香里ヶ丘の人気店や店舗情報をご紹介〜</p></div> 19 </div> 20 {% endblock %} 21 {% block main %} 22 <!-- Content Row--> 23 <div class="row gx-4 gx-lg-5"> 24 {% for object in object_list %} 25 <div class="col-md-4 mb-5"> 26 <div class="card h-100"> 27 <div class="card-body"> 28 <h2 class="card-title">{{ object.shop_name }}</h2> 29 <img src="{{ object.photo1.url }}" alt="" height=250 width=350> 30 <p class="card-text mt-3">{{ object.shop_describe }}</p> 31 </div> 32 <div class="card-footer"><a class="btn btn-primary btn-sm" href="{% url 'shoplist:detail' object.pk %}">詳しく</a></div> 33 </div> 34 </div> 35 {% empty %} 36 <p>登録されているお店がありません</p> 37 {% endfor %} 38 </div> 39 {% endblock %} 40 {% block footer %} 41 {% endblock %}
detail.html
html
1{% extends "base.html" %} 2{% load static %} 3 {% block header %} 4 <!-- Responsive navbar--> 5 <!-- Header--> 6 <header class="bg-dark py-5"> 7 <div class="container px-4 px-lg-5 my-5"> 8 <div class="text-center text-white"> 9 <h1 class="display-4 fw-bolder">{{ object.shop }}</h1> 10 <p class="lead fw-normal text-white-50 mb-0">With this shop hompeage template</p> 11 </div> 12 </div> 13 </header> 14 {% endblock %} 15 {% block main %} 16 {% for object in object_list %} 17 <!-- Content Row--> 18 <div class="row gx-4 gx-lg-5"> 19 <!-- Section--> 20 <section class="py-5"> 21 <div class="container px-4 px-lg-5 mt-5"> 22 <div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center"> 23 <div class="col mb-5"> 24 <div class="card h-100"> 25 <!-- Sale badge--> 26 <div class="badge bg-dark text-white position-absolute" style="top: 0.5rem; right: 0.5rem">Sale</div> 27 <!-- Product image--> 28 <img class="card-img-top" src="{{ object.product_photo.url }}" alt="..." /> 29 <!-- Product details--> 30 <div class="card-body p-4"> 31 <div class="text-center"> 32 <!-- Product name--> 33 <h5 class="fw-bolder">{{ object.product }}</h5> 34 <!-- Product reviews--> 35 <div class="d-flex justify-content-center small text-warning mb-2"> 36 <div class="bi-star-fill"></div> 37 <div class="bi-star-fill"></div> 38 <div class="bi-star-fill"></div> 39 <div class="bi-star-fill"></div> 40 <div class="bi-star-fill"></div> 41 </div> 42 <!-- Product price--> 43 <span>¥{{ object.price}}</span> 44 </div> 45 </div> 46 <!-- Product actions--> 47 <div class="card-footer p-4 pt-0 border-top-0 bg-transparent"> 48 <div class="text-center"><a class="btn btn-outline-dark mt-auto" href="#">Add to cart</a></div> 49 </div> 50 </div> 51 </div> 52 </div> 53 </div> 54 </section> 55 </div> 56 {% empty %} 57 <p>商品がありません</p> 58 {% endfor %} 59 {% endblock %} 60 {% block footer %} 61 {% endblock %}
models.py
python
1from django.db import models 2from django.db.models.deletion import PROTECT 3from django.db.models.fields import IntegerField 4 5# Create your models here. 6class Shop(models.Model): 7 #お店の概要 8 shop_name = models.CharField(verbose_name="店の名前", max_length=30) 9 shop_place = models.CharField(verbose_name="店の場所", max_length=60) 10 shop_style = models.CharField(verbose_name="お店の種類", max_length=10) 11 shop_describe = models.TextField(verbose_name="お店の詳細説明", max_length=300, default="説明がありません") 12 website = models.URLField(verbose_name="お店のホームページ", blank=True, null=True) 13 #photo1は縦横5:7の画像にするとぴったり 14 photo1 = models.ImageField(verbose_name="写真1", blank=True, null=True) 15 photo2 = models.ImageField(verbose_name="写真1", blank=True, null=True) 16 photo3 = models.ImageField(verbose_name="写真1", blank=True, null=True) 17 18 class Meta: 19 verbose_name_plural = "店舗情報" 20 21 def __str__(self): 22 return self.shop_name 23 24 25class Products(models.Model): 26 shop = models.ForeignKey(Shop, on_delete=PROTECT) 27 product = models.CharField(verbose_name="商品名", max_length=20) 28 price = models.IntegerField(verbose_name="価格") 29 product_detail = models.TextField(verbose_name="商品説明", max_length=150, blank=True, null=True) 30 #画像サイズは縦横2:3がベスト 31 product_photo = models.ImageField(verbose_name="商品写真") 32 33 class Meta: 34 verbose_name_plural = "商品情報" 35 36 def __str__(self): 37 return self.product