[英]Django form.as_p DateField not showing input type as date
Working on my first django app, and I have a model defined with some DateFields
, and then a ModelForm
off of that model ie在我的第一个 django 应用程序上工作,我有一个 model 定义了一些
DateFields
,然后是一个ModelForm
从那个 model 即
models.py模型.py
class MyModel(models.Model):... my_date = models.DateField('my date')... class MyModelForm(ModelForm): class Meta: model = MyModel fields = '__all__'
views.py视图.py
def show(request): form = MyModelForm template_name = 'myapp/show.html' return render(request,template_name,{'form':form})
and then in my html I use the .as_p
to have django render the form for me然后在我的 html 中,我使用
.as_p
让 django 为我呈现表单
<form action="/show/" method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit" /> </form>
But the DateFields
have input type text, not date.但是
DateFields
具有输入类型文本,而不是日期。 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() }
There's no need to subclass DateInput
.无需
DateInput
。
class MyModelForm(forms.ModelForm): class Meta: model = MyModel fields = '__all__' widgets = { 'my_date': forms.DateInput(attrs={'type': 'date'}) }
start_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
Using the django-widget-tweaks package you can do this pretty simply by using:使用django-widget-tweaks package 你可以很简单地做到这一点:
{% load widget_tweaks %} {{form.date|attr:"type:date"}}
and making the field a date time field in your class:并使该字段成为 class 中的日期时间字段:
date = forms.DateField()
Working on my first django app, and I have a model defined with some DateFields
, and then a ModelForm
off of that model ie在我的第一个Django应用程序上工作,我有一个定义了一些
DateFields
的模型,然后是那个模型的ModelForm
,即
models.py models.py
class MyModel(models.Model):
...
my_date = models.DateField('my date')
...
class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = '__all__'
views.py views.py
def show(request):
form = MyModelForm
template_name = 'myapp/show.html'
return render(request,template_name,{'form':form})
and then in my html I use the .as_p
to have django render the form for me然后在我的html中,使用
.as_p
让django为我呈现表单
<form action="/show/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
But the DateFields
have input type text, not date.但是
DateFields
具有输入类型的文本,而不是日期。 Is there a way to change this?有办法改变吗?
forms.py forms.py
from django import forms from.models import MyModel from django.forms.widgets import DateInput # need to import class MyForm(forms.ModelForm): class Meta: model = MyModel fields = '__all__' widgets = { 'my_date': DateInput(attrs={'type': 'date'}) }
To use directly in forms.Form在 forms.Form 中直接使用
class DateInput(forms.DateInput): input_type = 'date' class Gym(forms.Form): starting_date = forms.DateField(widget = DateInput)
class AddProduct(forms.Form): name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=True) amount = forms.DecimalField(widget=forms.NumberInput(attrs={'class': 'form-control'}), required=True) product_mfg = forms.CharField(widget=forms.DateInput(attrs={'class': 'form-control', 'type':'date'}), required=True) product_exp = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control', 'type':'date'}), required=True) department = forms.CharField(widget=forms.Select(attrs={'class': 'form-control'}), required=True)
pip install django-datetimepicker
pip install django-datetimepicker
'datetimepicker'
to your INSTALLED_APPS
'datetimepicker'
添加到您的INSTALLED_APPS
Here is an example of how to use the widget.这是一个如何使用小部件的示例。
. . Assign the
DateTimePicker
to a DateTimeField
, DateField
or TimeField
.将
DateTimePicker
分配给DateTimeField
、 DateField
或TimeField
。
from django import forms from datetimepicker.widgets import DateTimePicker class SampleForm(forms.Form): datetime = forms.DateTimeField(widget=DateTimePicker(),)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.