gpt4 book ai didi

python - Django 1.9 URLField 删除必要的 http ://prefix

转载 作者:行者123 更新时间:2023-11-28 22:33:40 26 4
gpt4 key购买 nike

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

这是我的模型:

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 的设置方式,我没有机会使用清理功能。 enter image description here

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

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

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

我看过这个帖子:django urlfield http prefix但不可否认,我不确定将它放在哪里以查看它是否有效。

最佳答案

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

关于python - Django 1.9 URLField 删除必要的 http ://prefix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39642003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com