Search and display results using calendar in Django

142 views
Skip to first unread message

Aakash Baranwal

unread,
May 1, 2019, 2:05:58 AM5/1/19
to django...@googlegroups.com
Hi Everybody,

I need help.

I want to perform search operation using calendars. I shall select two dates i.e. max_date which shall not be more than the current date but can be less and min_date, these two shall be the days range. The result fields are: in_count, out_count, and dwell_time. The output should sum up all the results of all the days which are between the max_date and min_date. How can I do that? Please help. Is there a need to define a model for this? I am completely clueless.  

Expected result is getting the output in the same form as in result_list.html with the result of all the fields of the days starting from min_date till max_date getting displayed and the min_date and max_date should also be shown in the template. Kindly help. 

My model :


class Result(models.Model):

    in_count   = models.PositiveIntegerField()
    out_count  = models.PositiveIntegerField()
    date_time  = models.DateTimeField()
    time       = models.TimeField()

    def __str__(self):
return "{},{},{},{}".format(self.in_count, self.out_count, self.date_time, self.time) 

My views.py

class ResultListView(ListView):

    def get_queryset(self, *args, **kwargs):
        qs = Result.objects.all()
        print(self.request.GET)
        query = self.request.GET.get("q", None)
        if query is not None:
            qs = qs.filter(
                Q(in_count__icontains=query) | Q(out_count__icontains=query) | Q(date_time__icontains=query) | Q(time__icontains=query))
        return qs

    def get_context_data(self, *args, **kwargs):
        context = super(ResultListView, self).get_context_data(*args, **kwargs)

        return context


class ResultDetailView(DetailView):
    queryset = Result.objects.all()

My result_list.html
<div class="card-header">Result on {{ object.date_time}}</div>
  <div class="card-body">
    <h3 class="card-title"><a  href="{% url 'result:result' %}"style="color: white; ">Result of Model</a></h3>
    <p class="card-text"> In Count {{ object.in_count }}</p>

    <p class="card-text">Out Count {{ object.out_count}}</p>

Aakash Baranwal

unread,
May 2, 2019, 2:05:51 AM5/2/19
to django...@googlegroups.com

My Calendar.html:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script> <div class="container"> <div class='col-md-5'> <div class="form-group"> <div class='input-group date' id='datetimepicker6'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> </div> <div class='col-md-5'> <div class="form-group"> <div class='input-group date' id='datetimepicker7'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> </div> </div> <script type="text/javascript"> jQuery(function () { jQuery('#datetimepicker6').datetimepicker(); jQuery('#datetimepicker7').datetimepicker({ useCurrent: false //Important! See issue #1075 }); jQuery("#datetimepicker6").on("dp.change", function (e) { jQuery('#datetimepicker7').data("DateTimePicker").minDate(e.date); }); jQuery("#datetimepicker7").on("dp.change", function (e) { jQuery('#datetimepicker6').data("DateTimePicker").maxDate(e.date); }); }); </script>

Now, I want to select multiple dates using this, say start_date and end_date which will filter the dates between that range and then sum the value of in_count and out_count and display it to the user.

So, far what I think I should use is, but I am not sure how to do it and display the result to the user. Someone please help me here.

Result.objects.filter(
    date_time__range=[start_date, end_date]
).aggregate(Sum('in_count'), Sum('out_count'))
Reply all
Reply to author
Forward
0 new messages