Created
June 4, 2020 07:33
-
-
Save ronaldgreeff/3b2da951245860f262408496c6ef36a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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