Генерация PDF с датами фильтрации

Я пытаюсь сгенерировать pdf с датами фильтрации с помощью модала. Если нет дат фильтрации, я могу сгенерировать PDF, но если есть дата фильтрации, возникает ошибка

[22/Oct/2022 08:59:38] "POST /generateinvoicevehicle/ HTTP/1.1" 405 0 Внутренняя ошибка сервера: /generateinvoicevehicle/

Если я обновляю страницу, "UnboundLocalError: local variable 'incident_vehicle' referenced before assignment [22/Oct/2022 08:59:41] "GET /generateinvoicevehicle/ HTTP/1.1" 500 72789"

Как правильно сформировать отчет с датами фильтрации? Спасибо

Виды

class GenerateInvoiceVehicle(View):
def get(self, request, *args, **kwargs):
    try:
        if request.method == 'POST':
            fromdate = request.POST.get('fromdate')
            todate = request.POST.get('todate')
            if fromdate:
                incident_general = IncidentGeneral.objects.filter(user_report__date__gte=fromdate)
                incident_vehicle = IncidentVehicle.objects.filter(incident_general__user_report__date__gte=fromdate)
            if todate:
                incident_general = IncidentGeneral.objects.filter(user_report__date__lte=todate)
                incident_vehicle = IncidentVehicle.objects.filter(incident_general__user_report__date__gte=fromdate)
            # incident_general_accident = IncidentGeneral.objects.filter(user_report__status = 2).values('accident_factor__category').annotate(Count('severity'), filter=Q(severity='Damage to Property'))
            incident_vehicle = incident_vehicle.filter(incident_general__user_report__status = 2)
            incident_vehicle1 = incident_vehicle.filter(incident_general__user_report__status = 2,incident_general__severity='Fatal' ).annotate(Count('incident_general__severity'))
            incident_vehicle2 = incident_vehicle.filter(incident_general__user_report__status = 2,incident_general__severity='Damage to Property' ).annotate(Count('incident_general__severity'))
            incident_vehicle3 = incident_vehicle.filter(incident_general__user_report__status = 2,incident_general__severity='Non-Fatal' ).annotate(Count('incident_general__severity'))
        # incident_general_classification = IncidentGeneral.objects.filter(user_report__status = 2, severity="Damage to Property").distinct('accident_factor')
       
        
    except:
        return HttpResponse("505 Not Found")
    data = {
        'incident_vehicle': incident_vehicle,
        # 'incident_general_classification': incident_general_classification,
        'incident_vehicle1': incident_vehicle1,
        'incident_vehicle2': incident_vehicle2,
        'incident_vehicle3': incident_vehicle3,
        # 'incident_general_collision3': incident_general_collision3,
        # 'amount': order_db.total_amount,
    }
    pdf = render_to_pdf('pages/generate_report_pdf_vehicle.html', data)
    #return HttpResponse(pdf, content_type='application/pdf')

    # force download
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "Vehicle_Classification.pdf" #%(data['incident_general.id'])
        content = "inline; filename='%s'" %(filename)
        #download = request.GET.get("download")
        #if download:
        content = "attachment; filename=%s" %(filename)
        response['Content-Disposition'] = content
        return response
    return HttpResponse("Not found")

HTML

<tbody>
                      <tr>
                          <td> <a href="{% url 'export_users_xls' %}"
                                  class="text-dark text-decoration-none">All Users</a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'export_accidentcausation_xls' %}" 
                                  class="text-dark text-decoration-none">Accident Factor vs Severity (XLS) </a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'export_classification_xls' %}" 
                                  class="text-dark text-decoration-none">Vehicle Classification vs Severity (XLS)</a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'export_collision_xls' %}" 
                                  class="text-dark text-decoration-none">Collision Type vs Severity (XLS)</a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'generateinvoice_accident' %}"
                                  class="text-dark text-decoration-none">Accident Factor vs Severity (PDF)</a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'generateinvoice_vehicle' %}" 
                                  class="text-dark text-decoration-none">Vehicle Classification vs Severity (PDF)</a></td>
                      </tr>
                      <tr>
                          <td> <a href="{% url 'generateinvoice_collision' %}"
                                  class="text-dark text-decoration-none">Collision Type vs Severity (PDF)</a></td>
                      </tr>
                      <tr>
                        <td> <a href="#selectDate" data-toggle="modal"
                                class="text-dark text-decoration-none">Report #10</a></td>
                        </tr>
                  </tbody>

Модальный

        <div id="selectDate" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="post" action="{% url 'generateinvoice_vehicle' %}">
                    {% csrf_token %}
                    <div class="modal-header">
                        <h4 class="modal-title">Filter Reports</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            <i class="fa-solid fa-xmark"></i>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>From</label>
                            <input type="date" name="fromdate" class="form-control date" required>
                        </div>
                        <div class="form-group">
                            <label>Until</label>
                            <input type="date" name="todate" class="form-control date" required>
                        </div>
                        <label class="text-danger">Please ensure that the correct dates has been selected.</label>
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                        <input type="submit" class="btn btn-primary" value="Done">
                    </div>
                </form>
            </div>
        </div>
    </div>
Вернуться на верх