RangeField and RangeWidget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django import forms
from django.template.loader import render_to_string
from django.forms.fields import EMPTY_VALUES
from django.utils.translation import ugettext as _

class RangeWidget(forms.MultiWidget):
    def __init__(self, widget, *args, **kwargs):
        widgets = (widget, widget)

        super(RangeWidget, self).__init__(widgets=widgets, *args, **kwargs)

    def decompress(self, value):
        return value

    def format_output(self, rendered_widgets):
        widget_context = {'min': rendered_widgets[0], 'max': rendered_widgets[1],}
        return render_to_string('widgets/range_widget.html', widget_context)

class RangeField(forms.MultiValueField):
    default_error_messages = {
        'invalid_start': _(u'Enter a valid start value.'),
        'invalid_end': _(u'Enter a valid end value.'),
    }

    def __init__(self, field_class, widget=forms.TextInput, *args, **kwargs):
        if not 'initial' in kwargs:
            kwargs['initial'] = ['','']

        fields = (field_class(), field_class())

        super(RangeField, self).__init__(
                fields=fields,
                widget=RangeWidget(widget),
                *args, **kwargs
                )

    def compress(self, data_list):
        if data_list:
            return [self.fields[0].clean(data_list[0]),self.fields[1].clean(data_list[1])]

        return None

More like this

  1. BRPhoneNumberWidget by semente 2 years, 7 months ago
  2. Widget for CommaSeparatedIntegerField with pre-defined choices by rudyryk 3 years, 10 months ago
  3. TinyMCE Widget by semente 4 years, 7 months ago
  4. Tamper safe HiddenFields by alexmeisel 5 years, 5 months ago
  5. Modifying the fields of a third/existing model class by marinho 3 years, 3 months ago

Comments

jjudge (on August 9, 2009):

Can we get an example of a 'widgets/range_widget.html'??

#

(Forgotten your password?)