Linked Questions

Popular Questions

Customize form validation error display in Django

Asked by At

When I use form.is_valid() Django automatically outputs the validation error message shown in the link below (circled in red). But what if I want to customize that message with custom logic but display it in the same way. How do I do that?

The page linked below allows you to register a phone number to a database. I want to add an unsubscribe page where the database is queried and if the phone number exists then delete the number. But if it doesn't exist in database, I want to display the message "Phone number is not currently registered."

https://imgur.com/SxqIt1N (sorry for external link, can't embed images yet)

models.py

class PhoneNumber(models.Model):
    country = models.CharField(max_length = 1)
    area = models.CharField(max_length = 3)
    phone_number = models.CharField(max_length = 7)
    reg_date = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ('country', 'area', 'phone_number')

    def add_to_database(self):
        self.save()

    def __str__(self):
        return str(self.country) + str(self.area) + str(self.phone_number)

Related Questions