Lef*_*fty 2 python django django-models django-urls django-forms
我已经看到了一堆关于这个的问题,但还没有找到答案。
这是我的模型:
class UserProfile(models.Model):
user = models.OneToOneField(User)
.
.
.
website = models.URLField(max_length=100, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
和我的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
Run Code Online (Sandbox Code Playgroud)
我试图清理网址,但我的 django 设置方式我没有机会获得清理功能。

如何更改 Django 以允许用户不输入 http(s):// 前缀?
我不想用这种类型的代码初始化它:
url = forms.URLField(max_length=200, help_text="input page URL", initial='http://')
Run Code Online (Sandbox Code Playgroud)
我看过这个帖子:django urlfield http prefix 但不可否认,我不确定把它放在哪里看它是否有效。
| 归档时间: |
|
| 查看次数: |
917 次 |
| 最近记录: |