Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: cc9bbd6b32
Fetching contributors…

Cannot retrieve contributors at this time

88 lines (61 sloc) 2.875 kB

Customization

Override default templates

Widgets have a template_name attribute that point to the template to use when rendering the form. Default templates are provided, for instance the default template for a TextInput and other input-type widgets is floppyforms/input.html. You can easily override this template in your project-level TEMPLATE_DIRS, assuming they take precedence over app-level templates.

Custom widgets with custom templates

If you want to override the rendering behaviour only for a few widgets, you can extend a Widget class from FloppyForms and override the template_name attribute:

import floppyforms as forms

class OtherEmailInput(forms.EmailInput):
    template_name = 'path/to/other_email.html'

Then, the output can be customized in other_email.html:

<input type="email"
       name="{{ name }}"
       id="{{ attrs.id }}"
       placeholder="john@example.com"
       {% if value %}value="{{ value }}"{% endif %}>

Here we have a hardcoded placeholder without needing to instantiate the widget with an attrs dictionary:

class EmailForm(forms.Form):
    email = forms.EmailField(widget=OtherEmailInput())

Adding more template variables

There is also a way to add extra context. This is done by subclassing the widget class and extending the get_context() method:

class OtherEmailInput(forms.EmailInput):
    template_name = 'path/to/other.html'

    def get_context(self, name, value, attrs):
        ctx = super(OtherEmailInput, self).get_context(name, value, attrs)
        ctx['foo'] = 'bar'
        return ctx

And then the other.html template can make use of the {{ foo }} context variable.

get_context() takes name, value and attrs as arguments, except for all Select widgets which take an additional choices argument.

Altering the widget's attrs

All widget attibutes except for type, name, value and required are put in the attrs context variable, which you can extend in get_context():

def get_context(self, name, value, attrs):
    ctx = super(MyWidget, self).get_context(name, value, attrs)
    ctx['attrs']['class'] = 'mywidget'
    return ctx

This will render the widget with an additional class="mywidget" attribute.

If you want only the attribute's key to be rendered, set it to True:

def get_context(self, name, value, attrs):
    ctx = super(MyWidget, self).get_context(name, value, attrs)
    ctx['attrs']['awesome'] = True
    return ctx

This will simply add awesome as a key-only attribute.

Jump to Line
Something went wrong with that request. Please try again.