2012-11-02 12 views
9

Prawdopodobnie jest to kiepskie pytanie, ale używam Django w UserCreationForm (nieznacznie zmodyfikowane, aby dołączyć e-mail), i chciałbym usunąć tekst help_text, który Django automatycznie wyświetla na stronie HTML.Usuwanie help_text z Django UserCreateForm

W części rejestru mojej strony HTML znajdują się pola Nazwa użytkownika, E-mail, Hasło1 & Hasło 2. Ale pod nazwą użytkownika jest "Wymagane: 30 znaków lub mniej, tylko litery, cyfry i @ ..." Pod hasłem Potwierdzenie (Hasło 2) jest napisane "Wprowadź to samo hasło, jak powyżej, w celu weryfikacji."

Jak mogę je usunąć?

#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> 

Odpowiedz

20

Można ustawić help_text pól sobie równych w __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() 

wyjściowa:

<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> 

Jeśli robisz zbyt wiele zmian, w takich przypadkach lepiej jest tylko ręcznym pola np

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

ale w twoim przypadku moje rozwiązanie będzie działać bardzo dobrze.

+1

niesamowite. dzięki za pomoc! –

2

Możesz dodać klasę css do pliku registration_form.html w ten sposób.

<style> 
 

 
.helptext{ 
 
    visibility: hidden; 
 
} 
 

 
</style>

3

Innym, czystsze opcją jest użycie słownika help_texts w klasie Meta. Przykład:

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

Więcej informacji tutaj: https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#overriding-the-default-fields

Works idealny dla użytkownika i adres e-mail, ale nie działa na hasło2. Nie mam pojęcia dlaczego.

+0

Pracowałem dla mnie dla ForeignKey. Dzięki! – texnic