Newbieee Newbieee - 2 months ago 18
Python Question

Django 1.9 URLField removing the necessary http:// prefix

I've seen a bunch of questions about this, but havent found an answer yet.

This is my models:

class UserProfile(models.Model):
user = models.OneToOneField(User)
.
.
.
website = models.URLField(max_length=100, blank=True, null=True)


and my forms.py:

class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('website')

def clean_website(self):
website = self.cleaned_data['website']
if website and not website.startswith('http://'):
website = 'http://' + website
return website

# def clean(self):
# cleaned_data = self.cleaned_data
# url = cleaned_data.get('url')
#
# if url and not url.startswith('http://'):
# url = 'http://' + url
# cleaned_data['url'] = url
# return cleaned_data


I've tried to clean the web address, but I the way django is set up I dont have an opportunity to get to the clean functions.
enter image description here

How can I change Django to allow user the ability to not put in the http(s):// prefix?

I don't want to initialize it with this type of code:

url = forms.URLField(max_length=200, help_text="input page URL", initial='http://')


I've seen this thread:django urlfield http prefix
But admittedly am not sure where to put it to see if it even works.

Answer

This validation isn't being done by Django, but by your browser itself. You can disable that by putting novalidate in the surrounding <form> element.