title: Rendering dynamic search in PDF xhtml2pdf Django
body: <p>I am having some problems in rendering a a search query in PDF in Django.
  What I am trying to achieve is to print in PDF a web page search (I am filtering some data basically) and print the results in PDF.</p>
  <p>I can be more clear with some code.</p>
  <p>views.py</p>
  <pre><code>def total_flight_hours(request):
      students = Student.objects.all()
      instructors = Instructor.objects.all()
      aircrafts = Aircraft.objects.all()
      start_date = request.POST.get('start_date')
      if not start_date:
          start_date = dt.now().date()
      end_date = request.POST.get('end_date')
      if not end_date:
          end_date = dt.now().date()
      student = request.POST.get('student')
      if not student:
          student = ''
      instructor = request.POST.get('instructor')
      if not instructor:
          instructor = ''
      aircraft = request.POST.get('aircraft')
      if not aircraft:
          aircraft = ''
  
      log_entry = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
          student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft))
      log_entry_dual = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
          student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft) &amp; Q(solo_flight=False))
      total_flight_hours = log_entry_dual.aggregate(eet=Sum(ExpressionWrapper(
          F('ata') - F('etd'), output_field=IntegerField()), output_field=DurationField()))['eet']
      if total_flight_hours != None:
          total_flight_hours = duration(total_flight_hours)
      log_entry_solo = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
          student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft) &amp; Q(solo_flight=True))
      total_flight_hours_solo = log_entry_solo.aggregate(eet=Sum(ExpressionWrapper(
          F('ata') - F('etd'), output_field=IntegerField()), output_field=DurationField()))['eet']
      if total_flight_hours_solo != None:
          total_flight_hours_solo = duration(total_flight_hours_solo)
  
      log_entry_list = zip(log_entry, log_entry_solo)
      student_mission = StudentMission.objects.filter(
          log_entry_id__in=log_entry)
  
      month_begin = dt.today().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
      today = dt.today().date()
      context = {
          'log_entry': log_entry,
          'log_entry_solo': log_entry_solo,
          'total_flight_hours': total_flight_hours,
          'total_flight_hours_solo': total_flight_hours_solo,
          'students': students,
          'instructors': instructors,
          'aircrafts': aircrafts,
          'student_mission': student_mission,
          'log_entry_list': log_entry_list,
          'month_begin': month_begin,
          'today': today
      }
      return render(request, 'flight/total_flight_hours.html', context)
  </code></pre>
  <p>The function above is the view for my page, which is working fine. What I'm trying to achieve is to have just the results of this page printed in PDF. I tried to do a view like that:
  views.py</p>
  <pre><code>class GenerateTotalFlightHoursPDF(View):
      def get(self, request, *args, **kwargs):
          template = get_template(
              'flight/total_flight_hours_render_pdf_view.html')
  
          students = Student.objects.all()
          instructors = Instructor.objects.all()
          aircrafts = Aircraft.objects.all()
          start_date = request.GET.get('start_date')
          if not start_date:
              start_date = dt.now().date()
          end_date = request.GET.get('end_date')
          if not end_date:
              end_date = dt.now().date()
          student = request.GET.get('student')
          if not student:
              student = ''
          instructor = request.GET.get('instructor')
          if not instructor:
              instructor = ''
          aircraft = request.GET.get('aircraft')
          if not aircraft:
              aircraft = ''
  
          log_entry = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
              student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft))
          log_entry_dual = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
              student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft) &amp; Q(solo_flight=False))
          total_flight_hours = log_entry_dual.aggregate(eet=Sum(ExpressionWrapper(
              F('ata') - F('etd'), output_field=IntegerField()), output_field=DurationField()))['eet']
          if total_flight_hours != None:
              total_flight_hours = duration(total_flight_hours)
          log_entry_solo = LogEntry.objects.filter(Q(date__range=[start_date, end_date]) &amp; Q(
              student_id__id__icontains=student) &amp; Q(instructor_id__id__icontains=instructor) &amp; Q(aircraft_id__id__icontains=aircraft) &amp; Q(solo_flight=True))
          total_flight_hours_solo = log_entry_solo.aggregate(eet=Sum(ExpressionWrapper(
              F('ata') - F('etd'), output_field=IntegerField()), output_field=DurationField()))['eet']
          if total_flight_hours_solo != None:
              total_flight_hours_solo = duration(total_flight_hours_solo)
  
          log_entry_list = zip(log_entry, log_entry_solo)
          student_mission = StudentMission.objects.filter(
              log_entry_id__in=log_entry)
          context = {
              'log_entry': log_entry,
              'log_entry_solo': log_entry_solo,
              'total_flight_hours': total_flight_hours,
              'total_flight_hours_solo': total_flight_hours_solo,
              'students': students,
              'instructors': instructors,
              'aircrafts': aircrafts,
              'student_mission': student_mission,
              'log_entry_list': log_entry_list,
          }
          html = template.render(context)
          pdf = render_to_pdf(
              'flight/total_flight_hours_render_pdf_view.html', context)
          return HttpResponse(pdf, content_type='application/pdf')
  </code></pre>
  <p>The problem is that the PDF file has the template printed but not the query results. I tried to find some information on the internet but I couldn't find anything.
  For reference I post also the urls and the pdf template</p>
  <p>urls.py</p>
  <pre><code>urlpatterns = [
      path('', views.aircraft_home, name='aircraft_home'),
      path('add_aircraft', views.add_aircraft, name='add_aircraft'),
      path('aircraft_list', views.AircraftListView.as_view(), name='aircraft_list'),
      path('add_new_flight', views.add_new_flight, name='add_new_flight'),
      path('edit_flight/&lt;str:pk&gt;', views.edit_flight, name='edit_flight'),
      path('delete_flight/&lt;str:pk&gt;', views.delete_flight, name='delete_flight'),
      path('flight_list', views.FlightListView.as_view(), name='flight_list'),
      path('total_flight_hours', views.total_flight_hours, name='total_flight_hours'),
  
      # PDF Views
      path('total_flight_hours_render_pdf_view', views.GenerateTotalFlightHoursPDF.as_view(),
           name='total_flight_hours_render_pdf_view')
  
  ]
  </code></pre>
  <p>template</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;table&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Date&lt;/th&gt;
          &lt;th&gt;Aircraft&lt;/th&gt;
          &lt;th&gt;Dep. AD&lt;/th&gt;
          &lt;th&gt;Arr. AD&lt;/th&gt;
          &lt;th&gt;ATD&lt;/th&gt;
          &lt;th&gt;ATA&lt;/th&gt;
          &lt;th&gt;Tot. Time&lt;/th&gt;
          &lt;th&gt;Solo Time&lt;/th&gt;
          &lt;th&gt;Instructor&lt;/th&gt;
          &lt;th&gt;Student&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          {% for log in log_entry %}
        &lt;tr&gt;
          &lt;td&gt;{{log.date|date:'d-m-Y'}}&lt;/td&gt;
          &lt;td&gt;{{log.aircraft}}&lt;/td&gt;
          &lt;td&gt;{{log.adep.icao_code}}&lt;/td&gt;
          &lt;td&gt;{{log.ades.icao_code}}&lt;/td&gt;
          &lt;td&gt;{{log.etd|time:'H:i'}}&lt;/td&gt;
          &lt;td&gt;{{log.ata|time:'H:i'}}&lt;/td&gt;
          &lt;td&gt;{% if log.solo_flight == False %}{{log.eet|date:'H:i'}}{% endif %}&lt;/td&gt;
          &lt;td&gt;{% if log.solo_flight == True %}{{log.eet|date:'H:i'}}{% endif %}&lt;/td&gt;
          &lt;td&gt;{{log.instructor.first_name}} {{log.instructor.last_name}}&lt;/td&gt;
          &lt;td&gt;{{log.student.first_name}} {{log.student.last_name}}&lt;/td&gt;
        &lt;/tr&gt; 
        {% endfor %}
      &lt;/tbody&gt;
      &lt;tr&gt;
          &lt;th scope=&quot;row&quot;&gt;Total Hours&lt;/th&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;{{total_flight_hours}}&lt;/td&gt;
          &lt;td&gt;{% if total_flight_hours_solo != None %}{{total_flight_hours_solo}}{% endif %}&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
  
      &lt;/tr&gt;
    &lt;/table&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>render_to_pdf</p>
  <pre><code>def render_to_pdf(template_src, context_dict={}):
      template = get_template(template_src)
      html = template.render(context_dict)
      result = BytesIO()
      pdf = pisa.pisaDocument(BytesIO(html.encode(&quot;ISO-8859-1&quot;)), result)
      if not pdf.err:
          return HttpResponse(result.getvalue(), 
      content_type='application/pdf')
      return None
  </code></pre>
  <p>Than you very much in advance for your kind help</p>