在我创建自定义注册表单模型

时间:2018-02-15 01:57:46

标签: python django

我正在尝试制作自定义注册表单,但遇到了麻烦。

我的forms.py有这个:

class RegisterForm(UserCreationForm):
    '''
    Form that makes user using just email as username
    '''
    error_messages= {
        "password_mismatch": _("Passwords do not match."),
        "duplicate_email": _("Email already exists."),
        "unique": _("Email already exists"),
    }
    register_username=  forms.EmailField(label=_("Email"), widget=forms.TextInput(attrs={"placeholder":"Email"}))
    register_password1= forms.CharField(label=_("Password"), widget=forms.PasswordInput(attrs={"placeholder":"Password"}))
    register_password2= forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput(attrs={"placeholder":"Confirm password"}))

    def clean_username(self):
            username = self.cleaned_data["username"]
            try:
                User._default_manager.get(username=username)

                #if the user exists, then let's raise an error message
                raise forms.ValidationError(self.error_messages['duplicate_email'],  #user my customized error message
                                            code='duplicate_email',   #set the error message key
                )
            except User.DoesNotExist:
                return username # great, this user does not exist so we can continue the registration process

    class Meta:
        model=  User
        fields= ("username",)

我的views.py看起来像这样:

def login_register(request, template="pages/login_register.html"):
    registration_form= RegisterForm()
    return render(request, template, {"registration_form": registration_form})

这导致我在login_register.html呈现的注册表格如下:

<form method="post">
 <input type="hidden" name="csrfmiddlewaretoken" value="CSRF-TOKEN-HERE">
 <p><input autofocus="" id="id_username" maxlength="150" name="username" type="text" required=""></p>
 <p><input id="id_password1" name="password1" type="password" required=""></p>
 <p><input id="id_password2" name="password2" type="password" required=""></p>
 <p><input id="id_register_username" name="register_username" placeholder="Email" type="text" required=""></p>
 <p><input id="id_register_password1" name="register_password1" placeholder="Password" type="password" required=""></p>
 <p><input id="id_register_password2" name="register_password2" placeholder="Confirm password" type="password" required=""></p> 
 <button class="btn btn-primary" type="submit">Register</button>
</form>

我只想要使用最后三个input标记。我的目标是让我的注册表格有三个字段:电子邮件,密码和密码确认。为什么Django会加载三个额外的顶级input字段,如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

#include <iostream> using namespace std; int binarySearch(int array[], int size, int searchValue) { cout << "BST with size = " << size << "[ "; for (int i = 0; i < size; ++i){ cout << array[i] << " "; } cout << " ]" << endl; if (size >= 1) { int mid = (size - 1) / 2; cout << "Mid " << mid << endl; if (array[mid] == searchValue) { return mid; } if (array[mid] > searchValue) { return binarySearch(array, mid, searchValue); } if (array[mid] < searchValue) { return binarySearch(array+mid+1, size-(mid + 1), searchValue); } } return -1; } int main() { int val, myNums[1000]; // Yuck--magic number! int pos, cnt = -1; cout << "Enter numbers from smallest to largest, 0 to stop\n"; do { cin >> myNums[++cnt]; } while (myNums[cnt] != 0); do { cout << "Enter number to search for: "; cin >> val; if (val != 0) { pos = binarySearch(myNums, cnt, val); cout << "binarySearch reported " << pos << endl; } } while (val != 0); return 0; } 已提供密码和密码确认。所以你只需要覆盖用户名字段,重命名register_username:

UserCreationForm