Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Django check form error code in template

I am using Django 1.10 and trying to find a way to check the error code of a form inside of the template. So I tried a few things, like errors.as_data or errors.as_json, but I was unable to parse the different values (except by using javascript). Could it be something like this ?

<p>{% for key, value in form.errors.items %}
                    {{ value }}
                    {% if code == 'inactive_account'%}
                         // do some stuff
                    {% endif %}
                {% endfor %}
</p>

But I don't know how to get this error code. Any suggestion ?

PS : I know that a solution would be to do it inside of the view, but since I am using a django already-made one, I would prefer not to do it.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The dictionary form.errors does not contain the ValidationError instances. You need to use the as_data method.

Note that you need to loop through the list of errors for each key, and then you can check the code.

{% for key, key_errors in form.errors.as_data.items %}
    {{ key }}
    {% for error in key_errors %}
        {% if error.code == 'inactive_account'%}
        // do some stuff
        {% endif %}
    {% endfor %}
{% endfor %}
over 4 years ago · Santiago Trujillo Report

0

There's also other way to check for the error code. You can use has_error to check for the error code.

{% form.has_error 'field_name' 'code' %}

To check for non-field errors use NON_FIELD_ERRORS as the field parameter.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!