Django calendar widget in a custom form

后端 未结 5 958
说谎
说谎 2021-01-31 04:25

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

5条回答
  •  礼貌的吻别
    2021-01-31 04:51

    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.

提交回复
热议问题