Django SelectDateWidget , SelectMonthYear and SelectDatetime Widgets

By default, Django comes with a SelectDateWidget which most people are familiar with. It looks like this:

from django.forms.extras.widgets import SelectDateWidget

class FooForm(forms.Form):
    date = form.(widget=SelectDateWidget())

Here are some useful snippets I’ve found if you wanted an output that is Month/Year or with time component.

  1. MonthYear
  2. DateTime

To use it, just create a new file called widgets.py (anyname will do but widgets makes most sense) in the app that would be using this widget. Import it as per normal into your forms document. If you’re going to be using both snippets in the same widgets.py document, be sure to update the __all__ field to include the classes you want to use. To use the above mentioned examples, it should look like this:

__all__ = ('MonthYearWidget', 'SplitSelectDateTimeWidget')
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s