-
-
Save diek/cfbcba42ede5bb91cae88d3fad45fe9b to your computer and use it in GitHub Desktop.
Automatic placeholder attributes from field labels in Django forms
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
from functools import partial | |
def placeholderify(form=None, fields=None): | |
""" | |
A decorator for Django forms that sets a `placeholder` attribute to all | |
fields. Each field's label is used as a placeholder. | |
Use it like so: | |
@placeholderify | |
class MyForm(forms.Form): | |
name = forms.CharField(label='Your name') | |
email = forms.EmailField(label='Your email') | |
Doing that, the `name` field will render like so: | |
<input ... placeholder="Your name"> | |
""" | |
if form is None: | |
return partial(placeholderify, fields=fields) | |
class WrappedForm(form): | |
def __init__(self, *args, **kwargs): | |
super(WrappedForm, self).__init__(*args, **kwargs) | |
if fields is None: | |
override = self.fields.values() | |
else: | |
override = [field for name, field in self.fields.items() if name in fields] | |
for field in override: | |
field.widget.attrs.setdefault('placeholder', field.label) | |
return WrappedForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment