2014-02-05 31 views
2

我想從django定製UserCreationForm。我做了以下定製usercreationform在django中的樣子

class myUserCreationForm(UserCreationForm): 


    class Meta: 
     model=User 
     fields = ('username', 'password1', 'password2') 
     widgets = { 
      'username':TextInput(attrs={'class':'form-control'}), 
      'password':TextInput(attrs={'class':'form-control'}), 
      'password2':TextInput(attrs={'class':'form-control'}), 
} 

但它不工作。當模板呈現時它創建的輸入框沒有附加的表單控件類。什麼可能是錯的?

+0

您確定已經包含[表單資產](https://docs.djangoproject.com/zh/dev/topics/forms/media/)嗎?你能發佈你正在使用的完整模板嗎? – arocks

+0

爲什麼我需要表單資源,我在模板中包含了css和js,但這些小部件沒有獲得該類。是不是用於定義CSS和JS的資產? – Apostolos

+0

我誤解了CSS類沒有得到渲染。您是否嘗試使用列出表單字段的聲明性表單? – arocks

回答

2

您需要從sctratch創建表單,它不應該擴展UserCreationForm。 UserCreationForm有一個明確定義的用戶名字段以及其他一些字段。你可以看看它here

+0

如果我在自定義表單的__init__的用戶名字段中添加了一個小部件,它會起作用嗎?我只需要添加一個小部件。我需要從頭開始寫一個表單嗎? – Apostolos

+1

看看https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L66 – dhana

+0

這就是我所做的,使用了那裏的代碼... Love OpenSource! ! – Apostolos

2

您應該覆蓋類Meta上的字段。這適用於我:

class CustomCreateUserForm(UserCreationForm): 

username = forms.RegexField(
    label=_("Login"), max_length=30, regex=r"^[\[email protected]+-]+$", 
    help_text=_("Required. 30 characters or fewer. Letters, digits and " 
       "@/./+/-/_ only."), 
    error_messages={ 
     'invalid': _("This value may contain only letters, numbers and " 
        "@/./+/-/_ characters.")}, 
    widget=TextInput(attrs={'class': 'form-control', 
          'required': 'true', 
          'placeholder': 'Login' 
    }) 
) 

password1 = forms.CharField(
    label=_("Password"), 
    widget=forms.PasswordInput(attrs={'class': 'form-control', 
             'required': 'true', 

    }) 
) 
password2 = forms.CharField(
    label=_("Password confirmation"), 
    widget=forms.PasswordInput(attrs={'class': 'form-control', 
             'type': 'password', 
             'required': 'true', 
    }), 
    help_text=_("Enter the same password as above, for verification.") 
) 

first_name = forms.CharField(
    label=_("Name"), 
    widget=forms.TextInput(attrs={'class': 'form-control', 
            'type': 'text', 
            'required': 'true', 
    }), 
    help_text=_("Enter user first and last name.") 
) 

email = forms.CharField(
    label=_("Email"), 
    widget=forms.TextInput(attrs={'class': 'form-control', 
            'type': 'email', 
            'placeholder': 'Email address', 
            'required': 'true' 
    }) 
) 

class Meta: 
     model = User 
+0

每個變量值之前的_是什麼?我的python和django版本不像(python 2.7 django 1.5) – Apostolos

+1

from django.utils.translation import ugettext_lazy as _ 這會自動添加字符串到翻譯 – arturex

6

剛剛偶然發現這一點,因爲我面臨着同樣的問題。你可以重寫myUserCreationForm類的init方法來設置表單的屬性。

class myUserCreationForm(UserCreationForm): 

class Meta: 
    model=User 
    fields = ('username', 'password1', 'password2') 

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

    self.fields['username'].widget.attrs['class'] = 'form-control' 
    self.fields['password1'].widget.attrs['class'] = 'form-control' 
    self.fields['password2'].widget.attrs['class'] = 'form-control' 
+1

輝煌 - 謝謝 – Michael