在Django

时间:2019-06-27 18:25:46

标签: python django django-models django-templates

这可能很简单,但确实让我头疼。我有一个链接到另一个模型ListingImages的多语言模型Listing。我正在检索Listing数据,并使用它来访问ListingImages的对象。但是,我没有得到期望的结果。这是我的文件。

models.py

class Listing(models.Model):
    listing_title = models.CharField(max_length=255)

class ListingImages(models.Model):
    listing = models.ForeignKey(Listing, on_delete=models.CASCADE)
    image_url = models.ImageField(upload_to=get_image_filename,
                          verbose_name='Listing Images')

    def get_image_filename(instance, filename):
        title = instance.listing.listing_title
        slug = slugify(title)
        return "listings_pics/%s-%s" % (slug, filename)

views.py

    def index(request):
        context = {
            'listings': Listing.objects.filter(status=True)
        }
        return render(request, 'base/index.html', context)

模板index.html

的片段
    <!--Listing start-->
<div class="property-item col-lg-3 col-md-6 col-12 mb-40">
    <div class="property-inner">
        <div class="image">
            <a href="single-properties.html"><img src="{{ listing.listingImages.image_url.url }}" alt=""></a>
            <ul class="property-feature">
                <li>
                    <span class="area"><img src="{% static 'base/assets/images/icons/area.png' %}" alt="">{{ listing.property.land_size }} SqFt</span>
                </li>
                <li>
                    <span class="bed"><img src="{% static 'base/assets/images/icons/bed.png' %}" alt="">{{ listing.property.bedrooms }}</span>
                </li>
                <li>
                    <span class="bath"><img src="{% static 'base/assets/images/icons/bath.png' %}" alt="">{{ listing.property.bathrooms }}</span>
                </li>
                <li>
                    <span class="parking"><img src="{% static 'base/assets/images/icons/parking.png' %}" alt="">{{ listing.property.car_spaces }}</span>
                </li>
            </ul>
        </div>
        <div class="content">
            <div class="left">
                <h3 class="title"><a href="single-properties.html">{{ listing.listing_title }}</a></h3>
                <span class="location"><img src="{% static 'base/assets/images/icons/marker.png' %}" alt="">{{ listing.city }}</span>
            </div>
            <div class="right">
                <div class="type-wrap">
                    <span class="price">Kshs {{ listing.property.price }}</span>
                    <span class="type">{{ listing.property.get_for_sale_rent_display }}</span>
                </div>
            </div>
        </div>
    </div>
</div>
<!--Listing end-->

但是,在模板中,我没有得到图像的URL,而是得到了<img src="(unknown)" alt="">。我没有做错什么,也不确定如何引用多字模型ListingImages,无论它应该是listingImages还是listing-images还是listing_images

1 个答案:

答案 0 :(得分:0)

您的问题的直接答案:正确的调用应该是listing.listingimages_set.all()-它会返回特定列表实例的所有列表图像的查询集。

此外,我建议不要将查询集传递给Django模板。我认为最好从python原语构建模板上下文并将其传递。这样,调试和推理起来就更容易了:

def index(request):
    listings = Listing.objects.filter(status=True).prefetch_related('listingimages_set')
    context = {
        'listings': [
            {
                'title': listing.listing_title,
                'images': list(listing.listingimages_set.all()),
            } for listing in listings
        ]
    }
    return render(request, 'base/index.html', context)

然后,您可以在模板中将列表的图片引用为

<ul>
{% for listing in listings %}
    <li>
        {{ listing.title }}
        <ul>
        {% for image in listing.images %}
            <li>{{ image.image_url }}</li>
        {% endfor %}
        </ul>
    </li>
{% endfor %}
</ul>

当然,这只是一个例子。但是它表明,通过这种方式,您可以更好地控制模板上下文,并且可以在其内容最终呈现在呈现的HTML页面中之前对其内容进行调试

顺便说一句,最常用和普遍采用的约定是用单个名词调用模型。例如。 ListingImage而不是ListingImages