1

I am building a system based on python django where user can register per district per state basis. Here I want to have a registration form like this, where first there will be a dropdown of states where one will select one state from it, then based on her selection, another dropdown of district will be populated, and then other field of the user like name, email, mobile_num has to be filled.

Now I have built one user registration form like following:

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

Here in the above form I cannot figure out how to handle district and state as per my requirement that I stated above so that when a user is saved she is associated with a given district and state. District dropdown will be dynamically populated based on her selection of state.

1

1 Answer 1

0

This might solve your problem. It has all details.

Django cities light

Not the answer you're looking for? Browse other questions tagged or ask your own question.