62
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()
}
50
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'})
}
- [Django]-DRF: custom ordering on related serializers
- [Django]-Django Rest Framework POST Update if existing or create
- [Django]-Is it bad to have my virtualenv directory inside my git repository?
- [Django]-How can I check database connection to mysql in django
- [Django]-Django 1.5 custom User model error. "Manager isn't available; User has been swapped"
- [Django]-Django: "TypeError: [] is not JSON serializable" Why?
5
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()
- [Django]-Setting the selected value on a Django forms.ChoiceField
- [Django]-Embed YouTube video – Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- [Django]-How to translate docker-compose.yml to Dockerrun.aws.json for Django
1
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'})
}
- [Django]-How to access data when form.is_valid() is false
- [Django]-Setting the selected value on a Django forms.ChoiceField
- [Django]-Count frequency of values in pandas DataFrame column
1
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)
- [Django]-What does on_delete do on Django models?
- [Django]-How do I use allow_tags in django 2.0 admin?
- [Django]-'WSGIRequest' object has no attribute 'user' Django admin
0
To use directly in forms.Form
class DateInput(forms.DateInput):
input_type = 'date'
class Gym(forms.Form):
starting_date = forms.DateField(widget = DateInput)
- [Django]-Django template comparing string
- [Django]-How do I set user field in form to the currently logged in user?
- [Django]-How to get Django and ReactJS to work together?
0
If we don’t use ModelForm
rather if we use django.views.generic.edit.CreateView
only, there is a way for converting the default type of the input field (from text to date).
class MyModelCreateView(CreateView):
model = MyModel
...
def get_form(self):
form = super().get_form()
form.fields['my_date'].widget = forms.DateInput(attrs={'type': 'date'})
return form
This will render the input field like this,
<input type="date" name="my_date" required="" id="id_my_date">
- [Django]-How to specify the login_required redirect url in django?
- [Django]-Generating file to download with Django
- [Django]-Create custom buttons in admin change_form in Django
-1
Installation
- Run
pip install django-datetimepicker
- Add
'datetimepicker'
to yourINSTALLED_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(),)
- [Django]-Function decorators with parameters on a class based view in Django
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-What is an efficient way of inserting thousands of records into an SQLite table using Django?
Source:stackexchange.com