【问题标题】:How do I print out ForeignKey models that are linked to a specific model in django?如何打印出链接到 django 中特定模型的 ForeignKey 模型?
【发布时间】:2019-07-11 09:38:38
【问题描述】:

我正在使用 django 模型来保存用户上传的图像。我希望将每个图像存储在 productimage 模型中,该模型是产品模型的外键模型,产品模型是用户模型的外键模型。我的意思是:

用户 -----> 产品 ------> 产品图片

我正在使用 for 循环从产品模型中打印出所有用户产品。然后对于产品模型中的每个产品,应该打印出与该特定产品模型相关联的 productimage ForeignKey 模型。但是,当我返回 productimage 模型时,它会返回所有 productimage 模型,而不仅仅是链接到该特定产品模型的模型。

为了进一步澄清我的问题,我的意思是,让我们对 ProductA 说,打印出附加到 ProductA 的所有 Productimages,但不是将它们全部打印出来,而是打印出所有 Productimages,无论它们链接到哪个产品模型。

models.py:
  class product(models.Model):
     user = models.ForeignKey(User, on_delete=models.CASCADE)
     product_title =  models.CharField(max_length=100, blank=True)
     product_price =  models.CharField(max_length=30, blank=True)
     product_description =  models.CharField(max_length=1000, blank=True)

 class productimage(models.Model):
     product = models.ForeignKey(product, on_delete=models.CASCADE)
     product_images = models.FileField(blank=True)


views.py:
 def products(request):
     template = loader.get_template("main/products.html")
     products = request.user.product_set.all()
     for product in products:
         productsimages = product.productimage_set.all()
         for productimage in productsimages:
             imageurl = productimage.product_images.url
     context = {
         "products" : products,
         "productsimages" : productsimages,
         "imageurl" : imageurl,
         }
     return HttpResponse(template.render(context,request))




 Html template:
   <div  id="noproducts">
   <!-- <img class="options" src="/static/main/images/box2.png"/>
   <h2>It seems like you don't have any products</h2> -->
   {% for product in products %}
   <div class="col-sm-12 col-lg-3" id="holla">
       <div id="imgcontainer">
         <img id="img" src="{{ imageurl }}">
       </div>
       <p>{{ product.product_title }}</p>
       <p>₦{{ product.product_price }}</p>
       <p>{{ product.product_description }}</p>
   </div>
   {% endfor %}
 </div>

【问题讨论】:

  • 您在 for 循环中反复覆盖 productimages 和 imageurl。因此,在上下文中, products 将拥有所有用户产品, productsimages 将拥有最后一个产品的 productimage 实例,而 imageurl 将拥有最后一个产品的最后一个 productimage 的 url ......这没有意义。相反,在视图中,您必须查询用户产品、预取产品图像并在模板中使用 for 循环。
  • 你能告诉我怎么做吗?
  • 试试我贴的代码
  • 另外我建议您在产品字段中添加一个related_name="images" 并将产品图像重命名为“路径”,这样您将获得更有意义的查询路线,例如... for image in product .images.all ...然后在forloop内部,它将是image.path.url。祝你好运

标签: django django-models


【解决方案1】:

在您的产品中添加另一个循环以显示其图像..

{% for product in products %}
   <div class="col-sm-12 col-lg-3" id="holla">
       <div id="imgcontainer">
         {%for product_image in product.productimage_set.all %}
           <img id="img" src="{{ product_image.product_images.url }}">
         {% endfor %}
       </div>
       <p>{{ product.product_title }}</p>
       <p>₦{{ product.product_price }}</p>
       <p>{{ product.product_description }}</p>
   </div>
{% endfor %}

【讨论】:

  • 哦,最后一件事,我如何让它只打印产品的每个产品图像的第一张图片?
  • 在这种情况下你不需要第二个 forloop.. 在 "
    "... 添加
【解决方案2】:

在 forloop 中您可以使用的产品:

{% for product in products %}
   <div class="col-sm-12 col-lg-3" id="holla">

    {% for p_img in product.productsimages_set.all %}

    <img src="{{ p_img.product_images.url }}">

    {% endfor %}
       <p>{{ product.product_title }}</p>
       <p>₦{{ product.product_price }}</p>
       <p>{{ product.product_description }}</p>
   </div>
   {% endfor %}

并从上下文中删除产品图片

如果仍然没有显示图像,请在控制台中检查是否显示了这些 img 标签,如果是,则您对图像的查找不正确,但这是另一个问题

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2019-04-30
    • 1970-01-01
    • 2017-12-15
    相关资源
    最近更新 更多