I am currently working on a project for an e-marketplace. In my database, I have set the ImageField as optional. However, I am facing difficulty in making it optional on the HTML page in the code snippet below:
<div class="mt-6 px-6 py-12 bg-gray-100 rounded-xl">
<h2 class="mb-12 text-2xl text-center"> Newest items </h2>
<div class="grid grid-cols-3 gap-3">
{%for item in items%}
<div>
<a href="#">
<div>
{ % if (item.image.url) % }
<img src='{{ item.image.url}}' class="rounded-t-xl">
{ % endif % }
</div>
<div class="p-6 bg-white rounded-b-xl">
<h2 class="text-2xl">{{item.name}}</h2>
<p class="text-gray-500">Price: {{item.price}}</p>
</div>
</a>
</div>
{% endfor %}
</div>
</div>
Could someone guide me on how to write the if statement accurately to retrieve item.image.url only when an image exists?
I attempted to add an if statement as demonstrated in the code above.
Update: I have managed to solve the issue. Updating this post in case someone else encounters a similar problem. The solution involved using if ...is not None.