首页 > 解决方案 > django DateField 是字符域

问题描述

我正在使用 django.forms ModelForm。我有 DoB,它是“日期字段”,但是,当我显示 DoB 的表单时,它像 charfield 一样显示。我应该怎么做才能让用户能够选择日期?任何帮助表示赞赏。谢谢

模型:

dob = models.DateField(validators=[MinValueValidator(18), MaxValueValidator(70)],blank=True, null = True, verbose_name="Day of birth")

HTML:

<form method = 'POST' >
    {{ edit_profile | crispy}}
    {% csrf_token %} 
    <button type = 'submit' name = 'submit' class = 'btn-edit'>Save</button>
</form>

在此处输入图像描述

标签: django

解决方案


I have faced an issue like this in the past. There is a very small fix for this. In your forms.py make the following changes:

from django.forms import ModelForm
from .models import Model_Name

class EquipmentForm(ModelForm):
    class Meta:
        model = Model_Name
        fields = 'dob'
        widgets = {'dob': DateInput()}

This should solve your issue


推荐阅读