简体   繁体   中英

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

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})

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>

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() }

There's no need to subclass 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:

 {% load widget_tweaks %} {{form.date|attr:"type:date"}}

and making the field a date time field in your 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

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})

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>

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

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

class DateInput(forms.DateInput): input_type = 'date' class Gym(forms.Form): starting_date = forms.DateField(widget = DateInput)

You can specify type attribute in attrs dictionary which you want.

 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)

Installation

  • Run pip install django-datetimepicker
  • Add 'datetimepicker' to your INSTALLED_APPS

Basic usage

Here is an example of how to use the widget.

. Assign the DateTimePicker to a DateTimeField , DateField or TimeField .

 from django import forms from datetimepicker.widgets import DateTimePicker class SampleForm(forms.Form): datetime = forms.DateTimeField(widget=DateTimePicker(),)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM