Html 如何在django中为每个帖子提取图像

Html 如何在django中为每个帖子提取图像,html,django,Html,Django,我有两种不同的型号。一个是帖子,另一个是图片。 我想要的是显示每个帖子的所有图片。以下是模型的文件: class Cars_Posts(models.Model): user = models.ForeignKey(MyUser, on_delete=models.CASCADE) post_city = models.ForeignKey(City, on_delete=models.CASCADE) post_title = models.CharField(max

我有两种不同的型号。一个是帖子,另一个是图片。 我想要的是显示每个帖子的所有图片。以下是模型的文件:

class Cars_Posts(models.Model):
    user = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    post_city = models.ForeignKey(City, on_delete=models.CASCADE)

    post_title = models.CharField(max_length=256)
    post_detail = models.TextField(max_length=65536)
    price = models.PositiveIntegerField()

    def __str__(self):
        return "%s %s %s %s %s"(
            self.user.id, self.post_city,
            self.post_title, self.post_detail, self.price,)



class Images_Cars(models.Model):
    post = models.ForeignKey(Cars_Posts, on_delete=models.CASCADE)
    car_images = models.ImageField(upload_to='car_images', blank=True, null=True )


    def __str__(self):
        return "%s %s " % (self.post_id, self.car_images, )
以下是用于搜索(查询数据库)的查看功能:

这两个文件运行时都没有错误。但是,在模板中,我想显示所有帖子和每个帖子的所有图像。我需要你的帮助。我反复查看结果,可以得到所有帖子。但我也得到了在每个循环中重复的每个帖子的所有图片。因此,我没有只获取与当前帖子相关的图像,而是获取了当前帖子和所有后续帖子中所有帖子的所有图像。任何帮助或建议都将不胜感激。此外,对于以高效和复杂的方式在视图文件中编写代码的任何建议,我们都将不胜感激

以下是HTML文件的一部分:

                {% csrf_token %}

                {% for item in result %}
                <li class="result-row">
                    <!-- image box -->

                    <span>

                        <a href="#" class="result-image-act" >
                            {% for image in all_images %}
                           <!-- if image list is empty display the default image  -->
                            {% if not image %}
                            <img  src="{% static 'path/to/default image' %}" class="active">

                            {% endif %}
                             <!-- I have class ="active" which is supposed to be class of first image  -->
                            {% if image and image.0 %}
                            <img  class="active" src="{{image.item.car_images.url}}" >

                            {% elif image and not image.0 %}
                            <img  src="{{image.item.car_images.url}}" >
                            {% endif %}
                            {% endfor %}
                        </a>




                        <span class="embed-result-price">{{item.price}}</span>
                        <div class="swipe-wrap">
                            <div class="swipe-wrap-lef">
                                <span class="move" >
                                    <div class="swipe-prev">
                                        <p>&lt;</p>
                                    </div>
                                </span>
                            </div>
                            <div class="swipe-wrap-rig">
                                <span class="move" >
                                    <div class="swipe-next">
                                        <p>&gt;</p>
                                    </div>
                                </span>
                            </div>
                        </div>

                    </span>



                        <span class="result-price">{{result.price}}

</span>
<span class="result-price">{{result.title}}</span>



                </li>

                {% endfor %}
{%csrf\u令牌%}
{result%%中的项的%1}
  • {{item.price}}

    {{result.price} {{result.title}
  • {%endfor%}

    注意:在html javascript代码中,它将为每一篇文章处理滑动图像,这就是为什么我为每一篇文章提取第一个图像,以向其添加class=“active”

    您可以在具有反向相关名称的模板中直接执行此操作:

    ImageCars
    模型中添加属性方法,以检查文件是否实际存在

    class Images_Cars(models.Model):
         ''' code '''
         car_images = models.ImageField(upload_to='car_images', blank=True, null=True )
    
         @property
         def car_images_exists(self):
             return self.car_images.storage.exists(self.car_images.name)
    
    模板

    {% for item in result %}
    
        {% for image in item.images_cars_set.all %}
            #<!-- The images_cars instance is here -->
    
            {% if image.car_images_exists %} # see changes in models
            {{image.car_images.url}}
            {% endif %}
        {% empty %}
             #<!--Item has no image_cars instances here: item.images_cars_set.all --> 
             <h3>No Images</h3>
        {% endfor %}
    
    {% endfor %}
    
    {result%]中项目的%
    {%用于item.images\u cars\u set.all%中的图像
    #
    {%if image.car_images_存在%}#查看模型中的更改
    {{image.car_images.url}
    {%endif%}
    {%empty%}
    # 
    没有图像
    {%endfor%}
    {%endfor%}
    
    您实际上不需要在
    视图中循环查询集
    ,然后在
    模板中循环查询集


    result
    作为模板变量,足以使模板中的所有内容都正常工作。我能知道如何检查特定帖子的图片是否为空,以便使用默认图片吗?。我使用了{%if-not-image%},但它工作不正常?我还使用了{%if-image-note%}。但还是一样。不显示默认图像。我确定路径为src=“{%static”path/to/default image”%}我还包括了{%load staticfiles%}您的意思是
    图像
    实例是否为空(没有图像实例)还是文件
    图像
    实际存在于服务器中?我都做了,所以您可以随意使用,或者可以全部使用它们。看我的最新消息谢谢你Lemayzeur。这是我想要的方式。
    {% for item in result %}
    
        {% for image in item.images_cars_set.all %}
            #<!-- The images_cars instance is here -->
    
            {% if image.car_images_exists %} # see changes in models
            {{image.car_images.url}}
            {% endif %}
        {% empty %}
             #<!--Item has no image_cars instances here: item.images_cars_set.all --> 
             <h3>No Images</h3>
        {% endfor %}
    
    {% endfor %}