form dateselector widget

Date: March 28th 2016
Last updated: March 28th 2016

Create drop down boxes to select day, month and year. This example uses the built-in functionality of django forms.

from datetime import date

class SurferForm(forms.ModelForm):
    # Create tuple containing years
    years = [(x) for x in range(1900,date.today().year+1)]

    # set field as datefield 
    # (by default this is a textfield)
    DOB = forms.DateField(label='Date of Birth',
        # create date selector 
        widget=forms.SelectDateWidget
            # provide the tuple of years created above
            (years = years,
            # Add label if no date is selected
            empty_label = ("Choose Year",
                         "Choose Month",
                         "Choose Day"),),)
    class Meta:
        model = Surfer
        fields = ['DOB',
                  'height', 
                  'weight', 
                  'hometown',
                  'stance']

results matching ""

    No results matching ""