Reiku
@Reiku (Rei k)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Djangoのモデルをテンプレートに表示したい

解決したいこと

DjangoのDetailというモデルをテンプレートに表示したいです。
Productというモデルはproduct_listというテンプレートに表示させられるのですが、
これと同じテンプレートにDetailモデルを表示させたいです。

発生している問題・エラー

HTML内で表示されない

models.py

from django.db import models
from django.urls import reverse
from django.shortcuts import render

#Detailモデル 今回表示したいもの

class Detail_tag(models.Model):
    name = models.CharField(max_length=250)

    def __str__(self):
        return self.name

class Detail(models.Model):

    class Meta:
        db_table = "detail"
    
    name = models.CharField(verbose_name='商品', max_length=255)
    slug = models.SlugField(verbose_name='URLに表示される名前', max_length=250)
    description = models.TextField(verbose_name='商品説明')
    price = models.IntegerField(verbose_name='価格')
    image = models.ImageField(upload_to='product')
    file = models.FileField(upload_to='product/file')
    stock = models.IntegerField(verbose_name='在庫')
    created = models.DateTimeField(auto_now=True)
    subtag = models.ManyToManyField(Detail_tag)

    def score(self):
        return self.name

    def __str__(self):
        return self.name


#Productモデル product_list.htmlに表示できているもの

class ValidManager(models.Manager):
    def get_query(self):
        return super(ValidManager, self).get_queryset().filter(available=True)

class PostTag(models.Model):
    name = models.CharField(max_length=200)
    
    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(verbose_name='コーディネート名', max_length=250, unique=True)
    slug = models.SlugField(verbose_name='URLに表示される名前', max_length=250, unique=True)
    description = models.TextField(verbose_name='説明', blank=True)
    price = models.IntegerField()
    image = models.ImageField(upload_to='product', blank=True)
    product_name = models.CharField(max_length=255, null=True)
    hover_image = models.ImageField(verbose_name='ポップアップの画像', upload_to='producthover', blank=True, )
    product_name2 = models.CharField(max_length=255, null=True)
    hover_image2 = models.ImageField(verbose_name='ポップアップの画像2', upload_to='producthover2', blank=True, )
    product_name3 = models.CharField(max_length=255, null=True)
    hover_image3 = models.ImageField(verbose_name='ポップアップの画像3', upload_to='producthover3', blank=True, )
    product_name4 = models.CharField(max_length=255, null=True)
    hover_image4 = models.ImageField(verbose_name='ポップアップの画像4', upload_to='producthover4', blank=True, )
    stock = models.IntegerField()
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    tags = models.ManyToManyField(PostTag)
    file = models.FileField(upload_to='file', blank=True)

    objects = models.Manager()

    valid_objects = ValidManager()

    class Meta:
        ordering = ('name',)
        verbose_name = 'product'
        verbose_name_plural = 'products'
    def __str__(self):
        return '{}'.format(self.name)

    def get_url(self):
        return reverse('shop:product_detail', args=[self.slug])

    def index(request):
        image_request = request.user.set_image_path
        return render(request, 'shop/base.html', {'image_request': image_request}) 

views.py

from django.shortcuts import render

from shop.models import Product, Detail

from django.core.paginator import Paginator, EmptyPage, InvalidPage



import code
console = code.InteractiveConsole(locals=locals()) 
console.interact()


def all_products(request):
    products_list = Product.valid_objects.all()

    paginator = Paginator(products_list, per_page=16)
    try:
        page = int(request.GET.get('page', '1'))
    except:
        page = 1

    try:
        products = paginator.page(page)
    except (EmptyPage, InvalidPage):
        products = paginator.page(paginator.num_pages)
        
    return render(request, 'shop/product_list.html', {'products': products})
0

1Answer

###自己解決いたしました。
details = Detail.objects.all()を
render内の辞書に追加しました。

"views.py"

def all_products(request):
    products_list = Product.valid_objects.all()
    details = Detail.objects.all()

    paginator = Paginator(products_list, per_page=16)
    try:
        page = int(request.GET.get('page', '1'))
    except:
        page = 1

    try:
        products = paginator.page(page)
    except (EmptyPage, InvalidPage):
        products = paginator.page(paginator.num_pages)

    data = {'products': products,
            'details': details
    }
        
    return render(request, 'shop/product_list.html', data)
0Like

Your answer might help someone💌