编辑:没有任何错误,此代码有效。
我正在使用django 1.4.5,我的表单没有显示我想要的所有字段:
我的代码如下:
class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'password1', 'password2')
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
if commit:
user.save()
return user
它只显示用户名,电子邮件和密码。
答案 0 :(得分:0)
所以我只是通过初始化表单并将其打印到终端来快速测试,我完全复制了您的表单代码并调用如下。
myregform = MyRegistrationForm()
print myregform
这会在终端
中返回以下响应<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" maxlength="30" name="username" type="text" /><br /><span class="helptext">Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.</span></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input id="id_email" name="email" type="text" /></td></tr>
<tr><th><label for="id_first_name">First name:</label></th><td><input id="id_first_name" maxlength="30" name="first_name" type="text" /></td></tr>
<tr><th><label for="id_last_name">Last name:</label></th><td><input id="id_last_name" maxlength="30" name="last_name" type="text" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><input id="id_password2" name="password2" type="password" /><br /><span class="helptext">Enter the same password as above, for verification.</span></td></tr>
正如您所看到的那样,first_name和last_name正在显示,所以我最好的猜测是它与您的模板处理代码有关。也许如果你也可以在这里发帖,我可以看看。