Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda