2

I have the following form:

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('title', 'dob')
        labels = {
            'dob': ('D.O.B'),
        }

Which is based on the following model:

class Profile(models.Model):
    user = models.OneToOneField(User)
    title = models.CharField(max_length=10)
    dob = models.DateField(max_length=8)

    class Meta:
        managed = True
        db_table = 'fbf_profile'

the HTML part is as follows:

            <div class="form-group">
                <label class="col-sm-5 control-label">{{ profileform.dob.label }}:</label>
                <div class="col-sm-7">
                    {{ profileform.dob }}
                    <div class="text-danger">
                        {% for error in profileform.dob.errors %}{{ error }}<br/>{% endfor %}
                    </div>
                </div>
            </div>

It is displaying correctly but in it's current format the user has to type their date of birth manually. I would like a date picker that allows them to choose day, month, year from three separate drop down boxes. Any idea on the best way to achieve this? Or if someone has any better ways they think selecting date of birth then I am open to ideas.

Many thanks, Alan.

Alan Tingey
  • 835
  • 11
  • 39

1 Answers1

6

You need to use DateInput widget from django.forms.widget.DateInput class for your field

from django.forms.widgets import DateInput


class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = ('title', 'dob')
    labels = {
        'dob': ('D.O.B'),
    }
    widgets = {
        'dob': DateInput(attrs={'type': 'date'})
    }
Subhanshu
  • 2,036
  • 1
  • 12
  • 17
  • I get the error: from django.forms.widget import DateInput ImportError: No module named widget. Thanks for the idea, will look into it. – Alan Tingey Jan 14 '17 at 09:40