2012-11-02 45 views
9

おそらくよくない質問ですが、私はDjangoのUserCreationForm(電子メールを含むように少し変更)を使用していますが、Djangoが自動的にHTMLページに表示するhelp_textを削除したいと思います。Djangoからhelp_textを削除するUserCreateForm

私のHTMLページの「登録」部分には、ユーザー名、電子メール、パスワード1 &パスワード2フィールドがあります。しかし、ユーザー名の下には「必須、30文字以下の文字、数字、@ ...のみ」パスワード確認(パスワード2)の下に「確認のために上記と同じパスワードを入力してください」と表示されます。

どうすれば削除できますか?

あなたが __init__

from django.contrib.auth.forms import UserCreationForm 
from django import forms 

class UserCreateForm(UserCreationForm): 
    email = forms.EmailField(required=True) 

    def __init__(self, *args, **kwargs): 
     super(UserCreateForm, self).__init__(*args, **kwargs) 

     for fieldname in ['username', 'password1', 'password2']: 
      self.fields[fieldname].help_text = None 

print UserCreateForm() 

出力にNoneにフィールドのhelp_textを設定することができ

#models.py 
class UserCreateForm(UserCreationForm): 
    email = forms.EmailField(required=True) 

    def save(self, commit=True): 
     user = super(UserCreateForm, self).save(commit=False) 
     user.email = self.cleaned_data['email'] 
     if commit: 
      user.save() 
     return user 

    class Meta: 
     model = User 
     fields = ("username", "email", "password1", "password2") 
     exclude = ('username.help_text') 

#views.py 
def index(request): 
    r = Movie.objects.all().order_by('-pub_date') 
    form = UserCreateForm() 
    return render_to_response('qanda/index.html', {'latest_movie_list': r, 'form':form},  context_instance = RequestContext(request)) 

#index.html 
<form action = "/home/register/" method = "post" id = "register">{% csrf_token %} 
    <h6> Create an account </h6> 
    {{ form.as_p }} 
    <input type = "submit" value = "Create!"> 
    <input type = "hidden" name = "next" value = "{{ next|escape }}" /> 
</form> 

答えて

20

:あなたはあまりにも多くの変更を行っている場合は、そのような場合にはそれだけでオーバーライドすることをお勧めし

<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" maxlength="30" /></td></tr> 
<tr><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr> 
<tr><th><label for="id_password2">Password confirmation:</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr> 
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr> 

フィールド

class UserCreateForm(UserCreationForm): 
    password2 = forms.CharField(label=_("Whatever"), widget=MyPasswordInput 

しかし、私のソリューションは非常にうまくいくでしょう。

+1

素晴らしい。助けてくれてありがとう! –

2

このように、registration_form.htmlファイルにcssクラスを追加できます。

<style> 
 

 
.helptext{ 
 
    visibility: hidden; 
 
} 
 

 
</style>

3

別、クリーナーのオプションは、クラスのMetaでhelp_texts辞書を使用することです。例:ここで

class UserCreateForm(UserCreationForm): 
    ... 
    class Meta: 
     model = User 
     fields = ("username", "email", "password1", "password2") 
     help_texts = { 
      'username': None, 
      'email': None, 
     } 

さらに詳しい情報:https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#overriding-the-default-fields

は、ユーザ名と電子メールのための完全な機能しますが、パスワード2は動作しません。理由は分かりません。

+0

ForeignKeyのために私のために働いた。ありがとう! – texnic

関連する問題