title: Django: Get dropdown value and select dynamic form fields for Class Based views
body: <p>In my current code (see below) I retrieve the selected value from a dropdown choicefield, e.g. the value '1'.</p>
  
  <pre><code>recipe_choice = request.GET.get('recipe_select', False)
  </code></pre>
  
  <p>I use this value in an IF logic to choose among different forms, such as </p>
  
  <pre><code>if recipe_choice == '1':
          ingredients_form = HamburgerForm(request.POST)
  </code></pre>
  
  <p>Then I return a Context that consist of my dropdown choicefield and selected form.</p>
  
  <p>It all works fine when I call it as a function and can use "Request" to extract my drop-down choice as a GET.</p>
  
  <p>E.g. as a function:</p>
  
  <pre><code>path('cooking/', cooking, name="cooking")
  </code></pre>
  
  <p>However, I would like to ask how I can get the same result when using Class Based FormView instead, Such as:</p>
  
  <pre><code>path('cooking/', CookingView.as_view(), name="cooking")
  </code></pre>
  
  <p>The problem I experience is that I cannot retrieve the selected value from a dropdown and cannot find it within the get_context_data to use it within an IF statement, thus cannot translate my old function into a new CookingView(FormView):</p>
  
  <pre><code>class CookingView(FormView):
      form_class = CookingForm
      template_name = 'form/cooking.html'
  
      def get_context_data(self, **kwargs):
  
          """
         # some logic to get a desired form, such as
  
          ingredients_form = []
          if recipe_choice == '1':
              ingredients_form = HamburgerForm(request.POST)
          elif recipe_choice == '2':
              ingredients_form = PancakeForm(request.POST)
  
          """
  
          context = super(CookingView, self).get_context_data(**kwargs)
          context['ingridients_form'] = ingredients_form
          return context
  </code></pre>
  
  <p>Q: How do I get this logic into a Class Based FormView?</p>
  
  <p><strong><em>Below I provide my current code that works (as a function).</em></strong></p>
  
  <p><strong><em>views.py</em></strong></p>
  
  <pre><code>def cooking(request):
      context = {}
      recipe_choice = request.GET.get('recipe_select', False)
  
      ingredients_form = []
      if recipe_choice == '1':
          ingredients_form = HamburgerForm(request.POST)
      elif recipe_choice == '2':
          ingredients_form = PancakeForm(request.POST)
  
      context['cookbook_form'] = CookingForm(request.GET or None)
      context['ingridients_form'] = ingredients_form
  
      return render(request, 'form/cooking.html', context)
  </code></pre>
  
  <p><strong><em>models.py</em></strong></p>
  
  <pre><code>class Ingridients(models.Model):
      # for hamburgers
      cheese  = models.IntegerField(default=0)
      ham     = models.IntegerField(default=0)
      onion   = models.IntegerField(default=0)
      bread   = models.IntegerField(default=0)
      ketchup = models.IntegerField(default=0)
      # for pancakes
      milk    = models.IntegerField(default=0)
      butter  = models.IntegerField(default=0)
      honey   = models.IntegerField(default=0)
      eggs    = models.IntegerField(default=0)
  </code></pre>
  
  <p><strong><em>forms.py</em></strong></p>
  
  <pre><code>class CookingForm(Form):
      RECIPES = (
          (1, 'Hamburger'),
          (2, 'Pancakes')
      )
      recipe_select = ChoiceField(choices=RECIPES)
  
  
  class HamburgerForm(forms.ModelForm):
      class Meta:
          model = Ingridients
          fields = ['cheese', 'ham', 'onion', 'bread', 'ketchup']
  
  class PancakeForm(forms.ModelForm):
      class Meta:
          model = Ingridients
          fields = ['milk', 'butter', 'honey', 'eggs']
  </code></pre>
  
  <p><strong><em>cooking.html</em></strong></p>
  
  <pre><code>{% load crispy_forms_tags %}
  &lt;!DOCTYPE html&gt;
  
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
    &lt;form id="cookselect_form" method="GET"&gt;
      {{ cookbook_form|crispy }}
      &lt;div class="btn-group btn-group-justified" role="group"&gt;
        &lt;div class="btn-group" role="group"&gt;
          &lt;button class="btn btn-primary" type="submit"&gt;Choose Recipe&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/form&gt;
  
    &lt;form id="ingridientslist_form" method="POST"&gt;
      {% csrf_token %}
      {% crispy ingridients_form %}
    &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>