Skip to content

Instantly share code, notes, and snippets.

@johndavidback
Created July 17, 2014 14:38
Show Gist options
  • Save johndavidback/fe17dc2a861fbf755691 to your computer and use it in GitHub Desktop.
Save johndavidback/fe17dc2a861fbf755691 to your computer and use it in GitHub Desktop.
Bootstrap Char Field for Django Form
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