Use add_error(None, ...) for General Form Errors

When you need to show a validation error that doesn't belong to a specific field, use add_error(None, message). This displays the error at the top of the form as a non-field error.

from django import forms
from django.utils.translation import gettext_lazy as _
from datetime import date

class DateRangeForm(forms.Form):
    start_date = forms.DateField(label=_("Start Date"))
    end_date = forms.DateField(label=_("End Date"))

    def clean(self):
        cleaned_data = super().clean()
        start_date = cleaned_data.get("start_date")
        end_date = cleaned_data.get("end_date")

        # Error on specific field
        if start_date and start_date < date.today():
            self.add_error("start_date", _("Start date cannot be in the past."))

        # General error - involves both fields equally
        if start_date and end_date and start_date > end_date:
            self.add_error(None, _("End date must be after start date."))

        return cleaned_data

Use add_error(None, ...): when the error relates to the form as a whole or involves multiple fields equally, rather than belonging to one specific field.

Tips and Tricks Programming Development Django 6.x Django 5.2 Django 4.2