1

Edit: there wasn't any mistake, this code works.

I am using django 1.4.5, My form is not displaying all the fields I want:

My code is the following:

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

It's only displaying the username, email and passwords.

4

1 回答 1

1

所以我只是通过初始化表单并将其打印到终端来快速测试这一点,我准确地复制了您的表单代码并调用如下。

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,所以我最好的猜测是它与您的模板处理代码有关。也许如果你也可以在这里发布,我可以看看。

于 2013-07-09T08:13:38.070 回答