Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% if special_knowledge.knowledgecost %}
{{ special_knowledge.knowledgecost|safe }}
{% else %}
Doesnt exist
{% endif %}

>Solution :

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

E.g:

{{ special_knowledge.knowledgecost|default_if_none:"nothing" }}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading