0

I'm new to Django and I would like to create a "modular registering form".

The aim of this form is to be able to select the needed form (related to the user type) to complete the registration of a new user.

models.py

class UserTypes(models.Model):
    USER_TYPES = (
        ('simple', 'simple'),
        ('advanced', 'advanced')
    )
    user_type = models.CharField(max_length = 10, choices = USER_TYPES)

class FirstClass(models.Model):
    username = models.CharField(max_length=100, null=True)
    first_name = models.CharField(max_length=100, null=True)
    last_name = models.CharField(max_length=100, null=True)

class SecondClass(models.Model):
    link_to_first_class = models.OneToOneField(FirstClass, on_delete=models.CASCADE, null=True)
    other_informations = models.CharField(max_length=200, null=True)

forms.py

class UserTypesForm(forms.ModelForm):
    class Meta:
        model = UserTypes
        fields = '__all__'

class FirstClassForm(forms.ModelForm):
    class Meta:
        model = FirstClass
        fields = '__all__'

class SecondClassForm(forms.ModelForm):
    class Meta:
        model = SecondClass
        fields = '__all__'
        exclude = ('link_to_first_class',)

views.py

@transaction.atomic
def modular_register(request):
    if request.method == 'POST':
        user_types = UserTypesForm(request.POST)
        first_form = FirstClassForm(request.POST)
        second_form = SecondClassForm(request.POST)

        if user_types_form.is_valid() and first_form.is_valid() and second_form.is_valid():
            first_form.save()

            first_class_instance = FirstClass.objects.filter(username = first_form.data['username'])[0]
            second_class = SecondClass(link_to_first_class = first_class_instance, 
                other_informations = second_form.data['other_informations'])
            second_class.save()

            messages.success(request, 'Account created successfully')

            return redirect('modularregister')

    else:
        user_types_form = UserTypesForm()
        first_form = FirstClassForm()
        second_form = SecondClassForm()

    return render(request, 'blog/modularregister.html', {'user_types_form':user_types_form, 'first_form': first_form, 'second_form' : second_form} )

template

 <form method="post" >
     {% csrf_token %}
     <table>
        {{ user_types_form.as_table }}
        {{ first_form.as_table }}
        {{ second_form.as_table }}
         <tr>
            <td><input type="submit" name="submit" value="Register" /></td>
        </tr>
     </table>
 </form>

Explanation :

If the user select "simple" as user_type only the FirstClassForm need to appear.

If the user select "advanced" the two forms (FirstClassForm and SecondClassForm) need to appear.

So I read some articles about the "OnChange" function which requires the writing of a script.

So here is my question :

According to you, what is the best way to select only the needed forms and how to handle that in the view ?

Mairkur
  • 177
  • 1
  • 15
  • I think it's best to involve some scripting to unhide SecondClassForm when advanced is checked. Check [this](https://stackoverflow.com/questions/17621515/how-to-show-and-hide-input-fields-based-on-radio-button-selection). – Cheche Oct 25 '18 at 11:26
  • you can hide the 2 forms (first and second) and onchange of user_type you can show the 1 form accordingly – Vaibhav Oct 25 '18 at 11:29

0 Answers0