0

I need to add RegexValidator to URLField. It works, but I want to customize it's message. I added parameter message with my custom text, but it still shows

Enter a valid URL." instead of my text

Django version: 2.1.5

class ExampleModel(models.Model):
    url_address = models.URLField(
        validators=[RegexValidator('https://www.google.com/.*', message='This is not a Google URL')])
5
  • 1
    add this code='invalid_url' as your last parameter of RegexValidator. Commented Jan 23, 2019 at 13:02
  • @Ahtisham this is a solution! Now my custom message is showing when regex check fails. Commented Jan 23, 2019 at 13:16
  • did that fixed the problem ? Commented Jan 23, 2019 at 13:18
  • @Ahtisham, yes, it is. Commented Jan 23, 2019 at 13:19
  • 1
    You can accept my edit to your question if you think it makes it better. :D Commented Jan 23, 2019 at 13:22

1 Answer 1

1

You need to add code as a third parameter to your RegexValidator

like this:

RegexValidator('https://www.google.com/.*', message='This is not a Google URL', code='invalid_url')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.