title: How to have dropdown selection populate DataTables table in template from Django model?
body: <p>Hopefully I can get some help with this. I've basically got a page on my site with a table, and the table is populated from the database with my models. My problem, is that I'd like to have a dropdown, where the user can select what data they want to see. I know that I need to use a django form in order to do this, but I've been searching, and have been having a really hard time finding what I need to do. The table is made with DataTables, and a bit of BS4 styling.</p>
  
  <p>Right now, I have the table being populated with just one of my models (Model1) and that's working great, but I'm not sure how to do what I want to do with making a selection from the dropdown, to populate with a different model. I kinda understand how forms work, but I'm not sure how to implement them how I want to. A lot of the stuff I see is just showing how to do a form with one model.</p>
  
  <p>Any sort of example, or guidance would be greatly appreciated :)</p>
  
  <p>Here's my models.py:</p>
  
  <pre><code>    class Model1(models.Model):
  
              date = models.DateTimeField(db_column='Date', primary_key=True, default='', max_length=40) 
              data1 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data1',)
              data2 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data2',)
              class Meta:
                      managed = True
                      db_table = 'table1'
              def __str__(self):
              return '%s %s %s' % (str(self.date), self.data1, self.data2,)
  
      class Model2(models.Model):
  
              date = models.DateTimeField(db_column='Date', primary_key=True, default='', max_length=40) 
              data1 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data1',)
              data2 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data2',)
              class Meta:
                      managed = True
                      db_table = 'table2'
              def __str__(self):
              return '%s %s %s' % (str(self.date), self.data1, self.data2,)
  
      class Model3(models.Model):
  
              date = models.DateTimeField(db_column='Date', primary_key=True, default='', max_length=40) 
              data1 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data1',)
              data2 = models.CharField(max_length=40, default='NoData', blank=True, null=True, db_column='data2',)
              class Meta:
                      managed = True
                      db_table = 'table3'
              def __str__(self):
              return '%s %s %s' % (str(self.date), self.data1, self.data2,)
  </code></pre>
  
  <p>Here's my views.py:</p>
  
  <pre><code>from app.models import Model1, Model2, Model3
  from django.shortcuts import render
  
  def Appview(request):
      template_name = 'app/app.html'
  
      Model1 = Model1.objects.all()
      Model2 = Model2.objects.all()
      Model3 = Model3.objects.all()
      return render(request, template_name, {'Model1': Model1})
  </code></pre>
  
  <p>Here's my (simplified) app.html:</p>
  
  <pre><code>&lt;div class="table-responsive container"&gt;
        &lt;table id = "table" class = "table table-sm table-hover" width="100%"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Date&lt;/th&gt;
                  &lt;th&gt;Data1&lt;/th&gt;
                  &lt;th&gt;Data2&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
  
          &lt;tbody&gt;
              {% for data in Model1 %}
              &lt;tr&gt;
                  &lt;td&gt;{{ data.date }}&lt;/td&gt; 
                  &lt;td&gt;{{ data.data1 }}&lt;/td&gt;
                  &lt;td&gt;{{ data.data2 }}&lt;/td&gt;    
              &lt;/tr&gt;
              {% endfor %}
          &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>The real gut of the question is this: How do I make a dropdown form that re-populates the view/template with a different model?</p>
  
  <p>EDIT: The models have many more fields, and the tables are much bigger than shown. I just trimmed things down for simplicity. Looking a bit deeper, I think I might need to use AJAX? </p>