我看到了很多关于此的问题,但还没有找到答案。
这是我的模型:
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
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.
如何更改 Django 以允许用户不输入 http(s):// 前缀?
我不想用这种类型的代码初始化它:
url = forms.URLField(max_length=200, help_text="input page URL", initial='http://')
我看过这个线程:django urlfield http 前缀 https://stackoverflow.com/questions/16307055/django-urlfield-http-prefix但不可否认的是,我不确定把它放在哪里看看它是否有效。
此验证不是由 Django 完成的,而是由浏览器本身完成的。您可以通过放置来禁用它novalidate
在周围的<form>
元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)