Integer Currency Input

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class IntegerCurrencyInput(forms.TextInput):
    def render(self, name, value, attrs=None):
        from django.contrib.humanize.templatetags.humanize import intcomma
        if value:
            value = "$%s" % intcomma(value)
        return super(IntegerCurrencyInput, self).render(name, value, attrs)

class IntegerCurrencyField(forms.IntegerField):
    widget = IntegerCurrencyInput
    
    def clean(self, value):
        if value:
            if value[0] == "$": value = value[1:] # Cut off the dollar sign
            value = value.replace(',', '') # Remove Commas
        value = super(IntegerCurrencyField, self).clean(value)
        return int(value) if value else value

More like this

  1. Newforms field for decimals with a comma by jonasvp 5 years, 3 months ago
  2. Currency Fields with newforms by sago 6 years, 2 months ago
  3. Allow any view (probably a generic view) to accept POST variables into extra_context by orblivion 2 years, 7 months ago
  4. (en-US) Humanized Decimal Field by ActionScripted 4 years, 11 months ago
  5. Currency Widget by Rupe 4 years ago

Comments

(Forgotten your password?)