本文介绍了DateTimeInput在django中呈现为简单文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从用户那里获取日期时间输入,但是日期输入的数据类型被设置为type = text

I have tried to take date time input from the user but data type of date input is being set as type="text"

以下是代码片段:

模板:

<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.min.css" integrity="sha256-DOS9W6NR+NFe1fUhEE0PGKY/fubbUCnOfTje2JMDw3Y=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js" integrity="sha256-FEqEelWI3WouFOo2VWP/uJfs1y8KJ++FLh2Lbqc8SJk=" crossorigin="anonymous"></script>


<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.stayFrom}}
    <script>
         $(function () {
         $("#id_stayFrom").datetimepicker({
             format: 'd/m/y',
             });
         });
      </script>
</form>

forms.py:

class RoomApplicationForm(forms.ModelForm):
    stayFrom = forms.DateTimeField(input_formats=['%d/%m/%y'])
    class Meta:
        model = Stay
        fields = ('stayFrom')

models.py:

models.py:

class Stay(models.Model):
    user = models.OneToOneField(Account, on_delete=models.CASCADE, primary_key = True)

    stayFrom = models.DateTimeField(verbose_name="stay start date")

此处是呈现的html:

heres the rendered html:

<input type="text" name="stayFrom" id="id_stayFrom" autocomplete="off">

您能指出我的代码中缺少什么吗?

can you point out what i'm missing in my code?

推荐答案

是一个文本字段()。因此,这是预期的行为。

The default widget for DateTimeField is a text field (DateTimeInput). So this is expected behavior.

这篇关于DateTimeInput在django中呈现为简单文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:58