Displaying uploaded images in the template - Django

前端 未结 2 1216
一整个雨季
一整个雨季 2021-01-03 08:34

I am trying to display uploaded images to a template for my imaginary vegetable catalogue.

I have a page to add a new vegetable and upload images. The main template

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 09:06

    You're trying to output the model itself, rather than the image field on the model.

    You probably want to use a different variable name in your for loop, or it may get confusing later trying to work out what "vegetable" refers to.

    Try

    {% if view %}
        

    Title: {{ vegetable.title }}

    Category: {{ vegetable.category }}

    Thumbnail: {{ vegetable.thumbnail }}

    Images: {% for vegetable_image in vegetable.images.all %} {{ vegetable_image.image.url }} {% endfor %}

    {% else %}

    no vegetables found - error

    {% endif %}

提交回复
热议问题