Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda