Get from and to date of daterange in flatpickr on form submit in django


```

How to get a value of daterange in flatpickr

<form method="post" id="apply_filter_form" onsubmit="apply_filter()"> {% csrf_token %} <div class="row"> <div class="col-md-6"> <div class="mb-3"> <label for="flatpickr-range" class="form-label ">Range Picker</label> <input type="text" class="form-control" placeholder="YYYY-MM-DD to YYYY-MM-DD" id="flatpickr-range" name="date_range" /> </div> </div> </div> <button type="submit" class="btn btn-primary my-3">submit</button> </form>

function

` function apply_filter() { $(document).on("submit", "#apply_filter_form", function (e) { e.preventDefault(); const csrftoken = $("[name=csrfmiddlewaretoken]").val();

    const date_range = $("#date_range").val();

    console.log(date_range)

    $.ajax({
      type: "POST",
      url: "/apply_filter/",
      headers: { "X-CSRFToken": csrftoken },
      data: {
        date_range: date_range,
      },
      success: function (response) {
        if (response["success"] === true) {
          $("#blogs_div").html(response.html);
        }
      },
      error: function () {
        alert("No Data Found to delete");
      },
    });
  });
}`

View

`def apply_filter(request):
    date_range = request.POST.get('date_range')
    print(date_range)
    data = dict()
    blogs = Blog.objects.filter(posted_by=request.user).order_by('-id')
    data['html'] = render_to_string('blog/blogs.html', {'blogs': blogs, })
    data["success"] = True
    return JsonResponse(data) 
`

Guys can you help me to fix this issue



How to get value of daterange field on form submit

Guys can you help me to solve the issue

Back to Top