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

Display error if there is error in fields

Im trying to raise error for empty fields or fields which are not validating in form
so Im doing this method below but I know this is not the best way…

views.py :

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

'KnowledgeForm': form,
'errors': str(form.errors),

but then in Django-template I have to use if for each field and im adding custom name for each field , i dont know why i cant use Verbose_name…

Template :

                    {% if errors %}
                    <div class="alert alert-danger">
                        <p>
                            {% if KnowledgeForm.errors.KnowledgeTitle %}
                            عنوان دانش: {{ KnowledgeForm.errors.KnowledgeTitle }}
                            {% endif %}
            
                            {% if KnowledgeForm.errors.KnowledgeTextSummary %}
                             Summary: {{ KnowledgeForm.errors.KnowledgeTextSummary }}
                            {% endif %}
            
                            {% if KnowledgeForm.errors.KnowledgeFromDate %}
                            from Date: {{ KnowledgeForm.errors.KnowledgeFromDate }}
                            {% endif %}
            
                            {% if KnowledgeForm.errors.KnowledgetoDate %}
                           To date : {{ KnowledgeForm.errors.KnowledgetoDate }}
                            {% endif %}
            
                            {% if KnowledgeForm.errors.KnowledgeProcess %}
                            Chart: {{ KnowledgeForm.errors.KnowledgeProcess }}
                            {% endif %}
            
                          {% endif %}

                        </p>
                    </div>
                    {% endif %}

Second method :

                    {% if KnowledgeForm.errors %}
                        <ul class="alert alert-danger">
                        {% for key,value in KnowledgeForm.errors.items %} 
                        <li>{{ key|escape }} : {{ value|escape }}</li>
                        {% endfor %}
                        </ul>
                    {% endif %}

in this method i get the name based on whats used in models.py how can i change it?

>Solution :

The most clear and concise way is to use a forloop

Try replacing your entire if block in your HTML with the below code

{% for field in KnowledgeForm %}
    {% if field.errors %}
        <div class="alert alert-danger">
             {{ field.label_tag }} {{ field.errors }}
        </div>
    {% endf %}
{% endfor %}

I don’t think you need 'errors': str(form.errors),

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