Skip to content

Instantly share code, notes, and snippets.

@saevarom
Created October 15, 2012 15:17
Show Gist options
  • Save saevarom/3893044 to your computer and use it in GitHub Desktop.
Save saevarom/3893044 to your computer and use it in GitHub Desktop.
PlaceholderAttributeMixin
class PlaceholderAttributeMixin(object):
def __init__(self, *args, **kwargs):
super(PlaceholderAttributeMixin, self).__init__(*args, **kwargs)
for field_name in self.fields:
field = self.fields.get(field_name)
if field:
if type(field.widget) in (forms.TextInput, forms.DateInput):
field.widget = forms.TextInput(attrs={'placeholder': field.label})
elif type(field.widget) in (forms.PasswordInput,):
field.widget = forms.PasswordInput(attrs={'placeholder': field.label})
# Usage:
from registration.forms import RegistrationForm
from helpers.forms import PlaceholderAttributeMixin
class PlaceholderRegistrationForm(PlaceholderAttributeMixin, RegistrationForm):
pass
## PlaceholderRegistrationForm now inserts html5 placeholder attribute in all text, date and password inputs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment