Hello there im trying to show a custom message like "Doesnt exists" if there isnt really any record to show
and ignore having None
in the template for empty records
Template :
<div >
<label style="color : blue;" ><i style="color : blue;" aria-hidden="true"></i> knowledge cost :</label>
<span >
{{ special_knowledge.knowledgecost|safe }}
</span>
</div>
<div >
<label style="color : blue;" ><i style="color : blue;" aria-hidden="true"></i> knowledge cost percemtage :</label>
<span >
{{ special_knowledge.knowledgecost_percent }}
</span>
</div>
based on the given HTML the first Field would Be None
becuase i dont have any record for it in my db
, so is there any more efficient way than using if
for each record ?
i tried this method for all to handle any empty record
{% if special_knowledge.knowledgecost %}
{{ special_knowledge.knowledgecost|safe }}
{% else %}
Doesnt exist
{% endif %}
CodePudding user response:
You can try to use Django default_if_none
template tag for this.
E.g:
{{ special_knowledge.knowledgecost|default_if_none:"nothing" }}
CodePudding user response:
First of: you could also use (Read more)
{% if special_knowledge.knowledgecost %}
{{ special_knowledge.knowledgecost|safe }}
{% empty %}
Doesnt exist
{% endif %}
But that is nearly the same :D
You can use the default
filter. (Read more)
{{ special_knowledge.knowledgecost|safe|default:"Doesnt exist" }}