Django 1.9 URLField 删除必要的 http:// 前缀

Django 1.9 URLField removing the necessary http:// prefix

我看到了很多关于这个的问题,但还没有找到答案。

这是我的模型:

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

和我的 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

我试过清理网址,但我没有机会使用清理功能。

如何更改 Django 以允许用户不输入 http(s):// 前缀?

我不想用这种代码初始化它:

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

我看过这个帖子: 但不可否认,我不确定把它放在哪里,看看它是否有效。

此验证不是由 Django 完成的,而是由您的浏览器本身完成的。您可以通过将 novalidate 放在周围的 <form> 元素中来禁用它。