Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django calendar widget in a custom form

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?

like image 461
MrGlass Avatar asked Mar 27 '11 13:03

MrGlass


2 Answers

Instead of the Admin widget, consider using the normal "SelectDateWidget" built into Django: https://docs.djangoproject.com/en/stable/ref/forms/widgets/#selectdatewidget

like image 126
rogueleaderr Avatar answered Oct 18 '22 15:10

rogueleaderr


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.

like image 12
davekr Avatar answered Oct 18 '22 13:10

davekr