Created
July 17, 2014 14:38
-
-
Save johndavidback/fe17dc2a861fbf755691 to your computer and use it in GitHub Desktop.
Bootstrap Char Field for Django Form
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 django import forms | |
class BootstrapCharField(forms.CharField): | |
def __init__(self, *args, **kwargs): | |
# Grab the placeholder from the kwargs if it exists | |
placeholder = kwargs.pop('placeholder') if 'placeholder' in kwargs else u'' | |
# Build all the defaults | |
super(BootstrapCharField, self).__init__(*args, **kwargs) | |
# Update the widget to add the Bootstrap class required for forms, .form-control, and apply placeholder | |
self.widget = forms.TextInput(attrs={'class': 'form-control', 'placeholder': placeholder}) | |
class SomeForm(forms.Form): | |
""" | |
A form that uses bootstrap-based form field, got tired of updating the widget constantly. | |
""" | |
name = BootstrapCharField(placeholder='Your name') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment