Skip to content

Instantly share code, notes, and snippets.

@victoraguilarc
Created November 16, 2013 08:24
Show Gist options
  • Save victoraguilarc/7497562 to your computer and use it in GitHub Desktop.
Save victoraguilarc/7497562 to your computer and use it in GitHub Desktop.
A icon prepended Field widget for crispy_forms, use mode: self.helper.layout = Layout( PrependedIconText('field_name', placeholder="Username", icon_class="fa fa-user"), )
# -*- encoding: utf-8 -*-
from crispy_forms.layout import Field
from crispy_forms.utils import TEMPLATE_PACK
class PrependedIconText(Field):
template = "fields/prepend_text_with_icon.html"
icon_class = "fa fa-user"
def __init__(self, *args, **kwargs):
if not hasattr(self, 'attrs'):
self.attrs = {}
if 'icon_class' in kwargs:
self.icon_class = kwargs.pop('icon_class')
super(PrependedIconText, self).__init__(*args, **kwargs)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
if hasattr(self, 'icon_class'):
context['icon_class'] = self.icon_class
return super(PrependedIconText, self).render(form, form_style, context, template_pack=template_pack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment