如何从Django生成的表单的一个输入字段向两个输入字段提供数据?

时间:2019-06-14 05:31:18

标签: django django-forms daterangepicker

我正在尝试在日期范围选择器上获得类似的外观,如下所示(图1)。 enter image description here

Figure 1 : Predefined Date Ranges

当前格式的HTML

<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="csrfmiddlewaretoken" value="">

<div id="div_id_start_date" class="form-group">
    <label for="id_start_date" class="col-form-label ">
        Start date
    </label>
    <div class="">
        <input type="text" name="start_date" value="2019-04-25" class="datetimeinput form-control" id="id_start_date"> </div>
</div>
<div id="div_id_end_date" class="form-group">
    <label for="id_end_date" class="col-form-label ">
        End date
    </label>
    <div class="">
        <input type="text" name="end_date" value="2019-04-26" class="datetimeinput form-control" id="id_end_date"> </div>
</div>

<input type="submit" id="btnsubmit" value="Submit">

HTML and JS of Figure 1 look

< div id = "reportrange" style = "background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%" >< i class = "fa fa-calendar" > < /i>&nbsp; < span > < /span > < i class = "fa fa-caret-down" > < /i > < /div >

< script type = "text/javascript" >
$(function() {

    var start = moment().subtract(29, 'days');
    var end = moment();

    function cb(start, end) {
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    }

    $('#reportrange').daterangepicker({
        startDate: start,
        endDate: end,
        ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }
    }, cb);

    cb(start, end);

}); < /script>

Django表单具有两个输入字段,但是示例中没有输入字段。如何获得当前表单所需的外观。

1 个答案:

答案 0 :(得分:0)

function cb(start, end) {
   $('#id_start_date').val(start.format('YYYY-MM-DD'));
   $('#id_end_date').val(end.format('YYYY-MM-DD'));
}

作者-Dan Grossman(daterangepicker的作者)