Django form.as_p DateField不显示输入类型作为日期 [英] Django form.as_p DateField not showing input type as date

查看:450
本文介绍了Django form.as_p DateField不显示输入类型作为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的第一个django应用程序,我有一个模型定义与一些 DateFields ,然后一个 ModelForm 关闭该模型,即

Working on my first django app, and I have a model defined with some DateFields, and then a ModelForm off of that model i.e.

models.py

class MyModel(models.Model):
    ...
    my_date = models.DateField('my date')
    ...

class MyModelForm(ModelForm):
    class Meta:
        model = MyModel
        fields = '__all__'

views.py

def show(request):
    form = MyModelForm
    template_name = 'myapp/show.html'
    return render(request,template_name,{'form':form})

然后在我的html中,我使用 .as_p 让django呈现我的表单

and then in my html I use the .as_p to have django render the form for me

<form action="/show/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

但是 DateFields 具有输入类型文本,不是日期。有没有办法改变这个?

But the DateFields have input type text, not date. Is there a way to change this?

推荐答案

你可以创建一个自定义的小部件:

You can create a custom widget:

from django import forms

class DateInput(forms.DateInput):
    input_type = 'date'

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = '__all__'
        widgets = {
            'my_date': DateInput()
        }

这篇关于Django form.as_p DateField不显示输入类型作为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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