Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronaldgreeff/3b2da951245860f262408496c6ef36a3 to your computer and use it in GitHub Desktop.
Save ronaldgreeff/3b2da951245860f262408496c6ef36a3 to your computer and use it in GitHub Desktop.
# note that in __init__ method, I have updated an HTML class attribute with form-control to every field of the
# form so that Bootstrap gets enabled on every field.
from .models import Friend
from django import forms
import datetime
class FriendForm(forms.ModelForm):
## change the widget of the date field.
dob = forms.DateField(
label='What is your birth date?',
# change the range of the years from 1980 to currentYear - 5
widget=forms.SelectDateWidget(years=range(1980, datetime.date.today().year-5))
)
def __init__(self, *args, **kwargs):
super(FriendForm, self).__init__(*args, **kwargs)
## add a "form-control" class to each form input
## for enabling bootstrap
for name in self.fields.keys():
self.fields[name].widget.attrs.update({
'class': 'form-control',
})
class Meta:
model = Friend
fields = ("__all__")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment