Django Forms DateInput小部件未填充 [英] Django Forms DateInput widget not populating

查看:69
本文介绍了Django Forms DateInput小部件未填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django编辑表单,该表单具有来自模型的许多字段,包括一些日期字段,对于合适的格式,我使用了DateInput小部件。不幸的是,当我们编辑对象时,所有其他字段已经填充了现有数据,但日期已填充。日期处于其初始状态(dd / mm / yyyy),由于是必需的,因此即使用户不想更改日期,用户也必须在每次要编辑对象时都重新输入日期。

I have a django edit form that has many fields from a model including some date fields, for a suitable format, I used a DateInput widget. Unfortunately, when we edit the object, all other fields are already populated with existing data, but the dates. The dates are in their initial state (dd/mm/yyyy) and since they are required, the user has to reenter the dates everytime they want to edit the object, even if they do not want to change the dates.

有人对如何在这些日期字段中预填充日期数据有任何想法吗?

does anyone have an idea on how to prepopulate the date data in these date fields?

forms.py :

forms.py:

 class DateInput(forms.DateInput):
        input_type = 'date'
        input_formats = ('%d-%m-%Y')
    [...]
 date_sinistre = forms.DateField(widget=DateInput, label='Date sinistre')
 date_effet = forms.DateField(widget=DateInput, label='Date effet')
 date_echeance = forms.DateField(widget=DateInput, label='Date échéance')

edit_object.html:

edit_object.html:

    <div class='row border border-primary rounded m-1 border-3'>
            <div class='col-md-4 p-0 pl-2 pr-2 justify-content-center align-self-center'>
                <strong>DATE DE SINISTRE:</strong>
            </div>
            <div class='col-md-8 p-0 pl-2 pr-0 justify-content-center align-self-center'>
                {{ dossierForm.date_sinistre|as_crispy_field }}
            </div>
        </div>

        <div class='row border border-primary rounded m-1 border-3'>
            <div class='col-md-4 p-0 pl-2 pr-2 justify-content-center align-self-center'>
                <strong>DATE EFFET:</strong>
            </div>
            <div class='col-md-8 p-0 pl-2 pr-0 justify-content-center align-self-center'>
                {{ dossierForm.date_effet|as_crispy_field }}
            </div>
        </div>

        <div class='row border border-primary rounded m-1 border-3'>
            <div class='col-md-4 p-0 pl-2 pr-2 justify-content-center align-self-center'>
                <strong>DATE ÉCHÉANCE:</strong>
            </div>
            <div class='col-md-8 p-0 pl-2 pr-0 justify-content-center align-self-center'>
                {{ dossierForm.date_echeance|as_crispy_field }}
            </div>
    </div>

PS:尽管我将格式指定为dd-mm-yy,但小部件仍显示日期

PS: although I specified the format to be dd-mm-yy, the widget still displays the date as MM-DD-YYYY.

截屏:

推荐答案

您的DateInput生成元素< input type = date> ,但是这种元素的格式为'%Y-%m-%d'

Your DateInput produces the element <input type = "date"> but this kind of element works with the format '%Y-%m-%d'.

实际上,检查正确的HTML元素应该像

In fact, inspecting a correct HTML element, it should be like this:

<input type="date" name="date" value="2020-04-07" id="id_date">

由于您以其他格式传递日期,因此初始值将被忽略。

Since your're passing a date in another format, the initial value is ignored.

以下应该工作

class DateInput(forms.DateInput):
        input_type = 'date'
        input_formats = ('%Y-%m-%d')
    [...]
 date_sinistre = forms.DateField(widget=DateInput, label='Date sinistre')
 date_effet = forms.DateField(widget=DateInput, label='Date effet')
 date_echeance = forms.DateField(widget=DateInput, label='Date échéance')

这篇关于Django Forms DateInput小部件未填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆