'Django form selection affect raising another form

I wana make form on add_form.html, where first appear selection of curve type and than the right form.So somehow link them together on one page. For example user choice: Static cyclic and under this form appears CyclicCurveForm. Thanks for help. models.py:

class CyclicCurve(models.Model):
    name = models.CharField(max_length=200, unique=True)
    K = models.FloatField(max_length=200)
    n = models.FloatField(max_length=200)
    comment = models.CharField(max_length=1000)
    material_id = models.ForeignKey(Material, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class EnCurve(models.Model):
    name = models.CharField(max_length=200, unique=True)
    Sf = models.FloatField()
    b = models.FloatField()
    c = models.FloatField()
    Ef = models.FloatField()
    comment = models.CharField(max_length=1000)
    material_id = models.ForeignKey(Material, on_delete=models.CASCADE)

forms.py:

class CurveForm(forms.Form):
    RELEVANCE_CHOICES = (
        (1, ("Static cyclic")),
        (2, ("En fatigue"))
    )
    type = forms.ChoiceField(choices=RELEVANCE_CHOICES, required=True)


class CyclicCurveForm(ModelForm):
    class Meta:
        model = CyclicCurve
        exclude = ['material_id']


class EnCurveForm(ModelForm):
    class Meta:
        model = EnCurve
        exclude = ['material_id']

add_form.html:

{% extends "main/base.html" %}
{% block title %}
Create
{% endblock %}
{% block content %}
<form action="" method="POST">
    {% csrf_token %}
    {{form.as_p}}
    <input type=button value="Previous Page" onClick="javascript:history.go(-1);">
    <input type="submit" name="Submit">
</form>

{% endblock %}

views.py:

###   CREATE/ADD/EDIT CURVE   ###
def create_curve(response, material_type_id, material_id):
    form = CurveForm()
    if response.method == "POST":
        form = CurveForm(response.POST)
        if form.is_valid():
            curve_type = form.cleaned_data
            .....

    context = {"form": form, "material_type_id": material_type_id, "material_id": material_id}
    return render(response, "main/add_update_form.html", context)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source