I am trying to add a calendar date input to one of my forms. There is a wonderful calendar widget that Django uses on its admin pages, but I can't figure out how to add it to a custom form.
I found a couple other questions about the same subject here, but they all seemed to be out of date, complicated, and not working.
Is there a way to include Django's built in calendar widget into one of my forms?
Instead of the Admin widget, consider using the normal "SelectDateWidget" built into Django: https://docs.djangoproject.com/en/stable/ref/forms/widgets/#selectdatewidget
All widgets used by django-admin are in the django/contrib/admin/widgets.py file. Just use that as a usual widget in a form. What you want is AdminDateWidget.
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField
class MyForm(Form):
my_field = DateField(widget=AdminDateWidget)
That will create a date-picker with calendar. You may try to customize it to what you need by extending it or create a new widget from a scratch with an inspiration from AdminDateWidget.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With