在Django中处理自定义用户注册表单中的动态下拉字段

时间:2016-03-04 13:37:57

标签: python django

我正在建立一个基于python django的系统,用户可以按地区每个地区注册。在这里,我希望有一个这样的注册表单,首先会有一个状态下拉列表,其中一个将从中选择一个状态,然后根据她的选择,将填充另一个区域下拉列表,然后是用户的其他字段喜欢名字,电子邮件,mobile_num必须填写。

现在我已经构建了一个用户注册表单,如下所示:

class RegistrationForm(UserCreationForm):

    district_id = <Yet to be implemented>
    state_id = <Yet to be implemented>
    first_name = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("First name"), error_messages={ 'invalid': _("This value must contain only letters") })
    last_name = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Last name"), error_messages={ 'invalid': _("This value must contain only letters") })
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))
    date_of_birth = forms.DateField(widget=forms.TextInput(attrs= {'class':'datepicker'}))
    sex = forms.ChoiceField(choices=(('M', 'MALE'), ('F', 'FEMALE')), label=_("Sex"))
    voter_id = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Voter Id"))
    is_election_staff = forms.BooleanField(initial=False, required=False)

    class Meta:
        model = CustomUser
        fields = ['first_name', 'last_name', 'voter_id', 'date_of_birth', 'sex', 'is_election_staff', 'district_id', 'state_id']


    def clean_username(self):
        try:
            user = User.objects.get(voter_id__iexact=self.cleaned_data['voter_id'])
        except User.DoesNotExist:
            return self.cleaned_data['voter_id']
        raise forms.ValidationError(_("The user with given voter id already exists. Please try another one."))



    def clean(self):
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields did not match."))
        return self.cleaned_data

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(RegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.date_of_birth = self.cleaned_data['date_of_birth']
        user.sex = self.cleaned_data['sex']
        user.voter_id = self.cleaned_data['voter_id']
        user.is_election_staff = self.cleaned_data['is_election_staff']
        user.username = user.voter_id
        # user.set_password(self.cleaned_data['password1'])

        if commit:
            user.save()
        return user

在上面的表格中,我无法弄清楚如何根据我上面提到的要求来处理地区和州,以便当用户被保存时,她与给定的地区和州相关联。区域下拉列表将根据她选择的状态动态填充。

1 个答案:

答案 0 :(得分:0)

这可能会解决您的问题。 它有所有细节。

Django cities light