Search code examples
djangotemplatesunique-constraint

display unique constraints errors message in html template dango


i'm trying to show unique constraints error in the template but i dont know to call back the error i know this works {{form.errors}} but it display the entire error messages in one place

class A(models.Model): 
    name = models.CharField()
    dob = models.DateTimeField()


    class Meta:
        constraints = [
             models.UniqueConstraint(fields=['dob','name'],name=_('full_information'))
        ]

for the other fields i use this in my template

{% if form.name.errors %}
    {{form.name.errors}}
 {% endif %}

but i dont know what should i do for the full_information error message ? thank you ...


Solution

  • You can work with the .non_field_errors method [Django-doc]:

    {% if form.non_field_errors %}
        {{ form.non_field_errors }}
    {% endif %}

    This will list the errors that are not bounded by a specific field.

    For more information, see the rendering fields manually section of the documentation.