简体   繁体   English

如何从Django中的日期选择器获取日期

[英]How to get the date from the date picker in django

I am trying to get the date from the user using date picker in django. 我正在尝试使用Django中的日期选择器从用户那里获取日期。 But I am just getting none despite user picking up a date . 但是,尽管用户选择了日期,但我什么也没得到。 I followed this tutorial https://simpleisbetterthancomplex.com/tutorial/2019/01/03/how-to-use-date-picker-with-django.html 我遵循了本教程https://simpleisbetterthancomplex.com/tutorial/2019/01/03/how-to-use-date-picker-with-django.html

I am trying to get the date using the cleaned.data. 我正在尝试使用cleaned.data获取日期。

date = form.cleaned_data['date']

How do I get the date user selects. 如何获得用户选择的日期。 Also, I want the hour and minute field in date picker 另外,我想要日期选择器中的小时和分钟字段

This is my date field in the form (I am using forms.form) 这是表单中的日期字段(我正在使用Forms.form)

 class DateForm(forms.Form):
    date = forms.DateTimeField(
        input_formats=['%d/%m/%Y %H:%M'],
        widget=forms.DateTimeInput(attrs={
            'class': 'form-control datetimepicker-input',
            'data-target': '#datetimepicker1'
        })
    )

Here is the code in my HTML file 这是我的HTML文件中的代码

<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
      <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1"/>
      <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
      </div>
    </div>

    <script>
      $(function () {
        $("#datetimepicker1").datetimepicker();
      });
    </script>

You have manually added the <input> to the HTML, but missed the important name parameter. 您已将<input>手动添加到HTML,但是错过了重要的name参数。 Without this, the field value isn't included in the POST and the server never sees it. 没有这个,字段值将不包含在POST中,服务器将永远不会看到它。

Add name="date" and it should work ok. 添加name="date" ,它应该可以正常运行。

The tutorial used {{ form.date }} for a reason - to make it harder to screw it up. 本教程使用{{ form.date }}是有原因的-使其变得更难安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM