2016-09-22 5 views
2

私はこれについてたくさんの質問を見ましたが、まだ答えは見つかりませんでした。Django 1.9必要なhttp://プレフィックスを削除するURLField

これは私のモデルである:

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 

私はウェブアドレスをきれいにしようとしましたが、私はいけない私ジャンゴが設定されている方法を持っていますクリーンな機能に到達する機会を提供します。 enter image description here

Djangoを変更して、ユーザーがhttp://プレフィックスを入れないようにする方法を教えてください。私はこのスレッドを見てきました

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

django urlfield http prefix をしかし、確かにそれも動作するかどうかを確認するためにそれをどこに置くかを確認していない

私はこの種のコードでそれを初期化する必要はありません。

答えて

3

この検証はDjangoではなく、ブラウザ自体によって行われています。これを無効にするには、<form>要素にnovalidateを入れます。

関連する問題