Reputation:
i would like to know how i can display a message like "Currently no Data Available" if the table is empty
<table>
<thead>
<tr style="font-size: small">
<th>Ranking</th>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Market Cap (USD)</th>
</tr>
</thead>
<tbody>
{% for price in price %}
<tr style="font-size: small">
<td>{{ price.rank }}</td>
<td>{{ price.symbol }}</td>
<td>{{ price.key|title }}</td>
<td>{{ price.value|intcomma }} $</td>
<td>{{ price.market_cap_usd|intcomma }} $</td>
</tr>
{% endfor %}
</tbody>
</table>
Thanks and Br
Upvotes: 1
Views: 1413
Reputation: 477265
Django templates have a for ... empty
[Django-doc] pattern for this. You can write {% for var in collection %} ... {% empty %} ... {% endfor %}
. In case the collection is not empty, then it will perform iteration and for each item in the collection render the body. In case the collection is empty it will render the bart between {% empty %}
and {% endfor %}
.
For example:
<table>
<thead>
<tr style="font-size: small">
<th>Ranking</th>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Market Cap (USD)</th>
</tr>
</thead>
<tbody>
{% for price in price %}
<tr style="font-size: small">
<td>{{ price.rank }}</td>
<td>{{ price.symbol }}</td>
<td>{{ price.key|title }}</td>
<td>{{ price.value|intcomma }} $</td>
<td>{{ price.market_cap_usd|intcomma }} $</td>
</tr>
{% empty %}
<tr><td colspan="5">Currently no Data Available</td></tr>
{% endfor %}
</tbody>
</table>
In case the collection is empty, then the {% empty %}
part is rendered.
Note: please rename your collection to
prices
, so{% for price in prices %}
, now you overwrite your template variable, and furthermoreprices
is more clear about what it contains.
Upvotes: 1