I am trying to verify that the submitted URL does not yet exist in the database.
The relevant parts of the Form class are as follows:
from django.contrib.sites.models import Site
class SignUpForm(forms.Form):
url = forms.URLField(label='URL for new site, eg: example.com')
def clean_url(self):
url = self.cleaned_data['url']
try:
a = Site.objects.get(domain=url)
except Site.DoesNotExist:
return url
else:
raise forms.ValidationError("That URL is already in the database. Please submit a unique URL.")
def clean(self):
The problem is that no matter what value I give, I can’t raise it ValidationError. And if I do something like this in a method clean_url():
if Site.objects.get(domain=url):
raise forms.ValidationError("That URL is already in the database. Please submit a unique URL.")
then I get an error DoesNotExist, even for URLs that already exist in the database. Any ideas?
source
share