我有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>
1条答案
按热度按时间kpbwa7wx1#
models.py
views.py
view_property.html