Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I verify if a URLField is from a desired domain in Django?

Tags:

forms

django

I have a URLField in my application form and I would like to check if the given URL is from www.domain.com.

Right now I'm using this solution in the clean method of the field, but I think that is not the best one...

    def clean_link(self):
    link = self.cleaned_data['link']
    if 'domain.com' not in link:
        raise forms.ValidationError('You only can add a domain.com URL.')

    return link

For example, if I enter http://www.thisismydomain.com would be correct. Any other solution?

Thank you very much.


1 Answers

you might want to have a look at the urlparse module, to avoid misses like

>>> 'good_domain.com' in 'http://evildomain.com/mylink#good_domain.com'
True
like image 128
second Avatar answered Dec 15 '25 22:12

second



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!