Custom message if there isnt any record to show in django-template

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 class="col-md-6 col-sm-12 col-xs-12 form-group pull-right ">
                        <label style="color : blue;" class="control-label col-md-5 col-sm-3 col-xs-12 pull-right size_Style"><i class="fa fa-circle" style="color : blue;" aria-hidden="true"></i> knowledge cost :</label>
                        <span class="col-md-12 col-sm-12 col-xs-12 form-group pull-right colorfull">
                            {{ special_knowledge.knowledgecost|safe }}
                        </span>
                        
                    </div>

                    <div class="col-md-6 col-sm-12 col-xs-12 form-group pull-right ">
                        <label style="color : blue;" class="control-label col-md-5 col-sm-3 col-xs-12 pull-right size_Style"><i class="fa fa-circle" style="color : blue;" aria-hidden="true"></i> knowledge cost percemtage :</label>
                        <span class="col-md-12 col-sm-12 col-xs-12 form-group pull-right colorfull">
                            {{ 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 %}

You can try to use Django default_if_none template tag for this.

E.g:

{{ special_knowledge.knowledgecost|default_if_none:"nothing" }}

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" }}

Back to Top