【问题标题】:Django 1.9 URLField removing the necessary http:// prefixDjango 1.9 URLField 删除必要的 http:// 前缀
【发布时间】:2017-01-31 04:57:03
【问题描述】:

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

这是我的模型:

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 的方式我没有机会获得清理功能。

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

我不想用这种类型的代码来初始化它:

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

我看过这个帖子:django urlfield http prefix 但诚然,我不确定把它放在哪里看它是否有效。

【问题讨论】:

    标签: python django django-models django-forms django-urls


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 2014-03-22
      • 2016-06-07
      • 2020-08-20
      • 2012-04-08
      • 2013-10-06
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多