django如何在多个项目中按id显示多张图片

lb3vh1jj  于 2023-07-01  发布在  Go
关注(0)|答案(1)|浏览(130)

我有2类在我的models.py

class Product(models.Model):
ext...
and
class ProductImage(models.Model):
product=models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
image=models.ImageField
views.py
def view_property(request, id):
    photo = get_object_or_404(ProductImage, id=id)
    product = get_object_or_404(Product, id=id)
    photos = ProductImage.objects.filter(product=product)
    context = {
        'photo':photo,
        'product':product,
        'photos': photos
    }
    return render(request,'products/view_property.html', context)

因此,为了在我的HTML页面中显示多个图像,我在www.example.com url.py中包含int:id或类似的东西url.py

from django.urls import path
from . import views

urlpatterns = [
    path( '<int:id>/' , views.view_property , name = 'view_property' ),
]

现在,当我想获取数据到我的view_property.html页面,我得到的图像过滤的ID罚款,所以它的工作原理,但问题是在我的view_property.html页面在页面上我有项目,每个项目包含的产品和图片在滑块,所以我想获取多个产品每个产品获取他的图像过滤ID view_property.html

<div class="box-container">
        {% for pro in products %}
        <div class="box">
            <div class="image-container">
                {% for img in photos %}
                <div class="mySlides"><img src="{{img.image.url}}" alt=""></div>
                {% endfor %}
            </div>
            <div class="content">
                <div class="location">
                    <p>apartments</p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

她我得到一个产品的所有图像与每个项目相同的事情,因为我从url int:id获取一个产品的数据
我认为变化将在url.pyviews.py我希望我得到一个答案
我试着从www.example.com删除url.py

from django.urls import path
from . import views

urlpatterns = [
    path( 'view_property/' , views.view_property , name = 'view_property' ),
]

我想在view_property.html页面中添加int:id,正好在for循环中,但我不知道怎么做,但我尝试了这个例子:

<div class="box-container">
        {% for pro in products ***<int:id>***%}
        <div class="box">
            <div class="image-container">
                {% for img in photos %}
                <div class="mySlides"><img src="{{img.image.url}}" alt=""></div>
                {% endfor %}
            </div>
            <div class="content">
                <div class="location">
                    <p>apartments</p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
kpbwa7wx

kpbwa7wx1#

models.py

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=200, blank=True, null=True)
    image = models.ImageField(upload_to='images/product', default='')

    def __str__(self):
        return self.name

class ProductImage(models.Model):
    product = models.ForeignKey(Product, on_delete=models.DO_NOTHING, blank=True, null=True)
    image = models.ImageField(upload_to='images/product_images', default='')

views.py

def view_property(request, id):
    product = get_object_or_404(Product, id=id)
    photos = ProductImage.objects.all().filter(product=product.id)
    context = {
        'photo': product.image,
        'product': product,
        'photos': photos
    }
    return render(request,'products/view_property.html', context)

view_property.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Product Detail</title>
</head>
<body>
    <p>
        Product Image:
    </p>
    <img src="{{photo.url}}" alt="" srcset="">
    <p>
        {{product}}
    </p>
    <p>
        Product Images:
    </p>
    {% for i in photos %}
        <img src="{{i.image.url}}" alt="" srcset="">
    {% endfor %}
</body>
</html>

相关问题