title: Making a multiple field search in Django
body: <p>I am trying to make a search page that searches for model objects using three fields corresponding to different pieces of data. Here is my code below:</p>
  
  <p>models.py</p>
  
  <pre><code>class Schedules(models.Model):
      course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
      start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
      instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def search_Schedule(request):
      context_dict = {}
      if request.method == 'POST':
          query1 = request.POST['course_name_search']
          query2 = request.POST['start_date_search']
          query3 = request.POST['instructor_search']
          if query1:
              results = Schedules.objects.filter(course_name__icontains=query1)
              if query2:
                  results = results.filter(start_time=query2)
                  if query3:
                      results = results.filter(instructor__icontains=query3)
                      table = ScheduleTable(results)
                      if results.count():
                          context_dict['table'] = table
                      else:
                          context_dict['no_results'] = query1 + ", " + query2 + ", and " + query3
                  else:
                      table = ScheduleTable(results)
                      if results.count():
                          context_dict['table'] = table
                      else:
                          context_dict['no_results'] = query1 + " and " + query2
              elif query3:
                  results = results.filter(start_time__icontains=query3)
                  table = ScheduleTable(results)
                  if results.count():
                      context_dict['table'] = table
                  else:
                      context_dict['no_results'] = query1 + " and " + query3
              else:
                  table = ScheduleTable(results)
                  if results.count():
                      context_dict['table'] = table
                  else:
                      context_dict['no_results'] = query1
          elif query2:
              results = Schedules.objects.filter(start_time=query2)
              if query3:
                  results = results.filter(instructor__icontains=query3)
                  table = ScheduleTable(results)
                  if results.count():
                      context_dict['table'] = table
                  else:
                      context_dict['no_results'] = query2 + " and " + query3
              else:
                  table = ScheduleTable(results)
                  if results.count():
                      context_dict['table'] = table
                  else:
                      context_dict['no_results'] = query2
          elif query3:
              results = Schedules.objects.filter(instructor__icontains=query3)
              table = ScheduleTable(results)
              if results.count():
                  context_dict['table'] = table
              else:
                  context_dict['no_results'] = query3
      return render(request, "schedule/search_schedule.html", context_dict)
  </code></pre>
  
  <p>search_schedule.html</p>
  
  <pre><code>{% block main_content %}
      &lt;form method="post" action=""&gt;
          {% csrf_token %}
          &lt;label for="course_name_search"&gt;Course Name:&lt;/label&gt;
          &lt;input type="text" name="course_name_search" id="course_name_search"&gt;
  
          &lt;label for="start_date_search"&gt;Start Date:&lt;/label&gt;
          &lt;input type="datetime" name="start_date_search" id="start_date_search"&gt;
  
          &lt;label for="instructor_search"&gt;Instructor:&lt;/label&gt;
          &lt;input type="text" name="instructor_search" id="instructor_search"&gt;&lt;br&gt;
          &lt;input type="submit" name="submit"&gt;
      &lt;/form&gt;
      &lt;div id="result_panel"&gt;
          {% if table %}
              {% render_table table %}
          {% else %}
              {% if no_results %}
                  No results returned for &lt;q&gt;{{ no_results }}&lt;/q&gt;
              {% else %}
                  Please enter a search
              {% endif %}
          {% endif %}
  {% endblock %}
  </code></pre>
  
  <p>For some reason, the search will work if I type in the course name field OR the instructor field, but won't work if I type in more than one field. And for some reason, the start date field will not work period, no matter how I type the date. Can somebody possibly help me with the correct way to type the code? Thank you.</p>