Python django.forms.NumberInput() Examples

The following are 9 code examples of django.forms.NumberInput(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms , or try the search function .
Example #1
Source File: test_floatfield.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_floatfield_widget_attrs(self):
        f = FloatField(widget=NumberInput(attrs={'step': 0.01, 'max': 1.0, 'min': 0.0}))
        self.assertWidgetRendersTo(
            f,
            '<input step="0.01" name="f" min="0.0" max="1.0" type="number" id="id_f" required>',
        ) 
Example #2
Source File: forms.py    From django-admin-numeric-filter with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        name = kwargs.pop('name')
        super().__init__(*args, **kwargs)

        self.fields[name] = forms.FloatField(label='', required=False,
            widget=forms.NumberInput(attrs={'placeholder': _('Value')})) 
Example #3
Source File: forms.py    From django-admin-numeric-filter with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        self.name = kwargs.pop('name')
        super().__init__(*args, **kwargs)

        self.fields[self.name + '_from'] = forms.FloatField(label='', required=False,
            widget=forms.NumberInput(attrs={'placeholder': _('From')}))
        self.fields[self.name + '_to'] = forms.FloatField(label='', required=False,
            widget=forms.NumberInput(attrs={'placeholder': _('To')})) 
Example #4
Source File: forms.py    From yournextrepresentative with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        election = kwargs.pop('election')
        post = kwargs.pop('post')
        super(AddCandidacyPickPartyForm, self).__init__(*args, **kwargs)
        party_set = PartySet.objects.get(postextra__slug=post)

        self.fields['party'] = \
            forms.ChoiceField(
                label=_("Party in {election}").format(
                    election=election.name,
                ),
                choices=party_set.party_choices(),
                required=False,
                widget=forms.Select(
                    attrs={
                        'class': 'party-select party-select-' + election.slug
                    }
                ),
            )
        if election.party_lists_in_use:
            # Then add a field to enter the position on the party list
            # as an integer:
            self.fields['party_list_position'] = forms.IntegerField(
                label=_("Position in party list ('1' for first, '2' for second, etc.)"),
                min_value=1,
                required=False,
                widget=forms.NumberInput(
                    attrs={
                        'class': 'party-position party-position-' + election.slug
                    }
                )
            ) 
Example #5
Source File: test_decimalfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_decimalfield_widget_attrs(self):
        f = DecimalField(max_digits=6, decimal_places=2)
        self.assertEqual(f.widget_attrs(Widget()), {})
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '0.01'})
        f = DecimalField(max_digits=10, decimal_places=0)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1'})
        f = DecimalField(max_digits=19, decimal_places=19)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'})
        f = DecimalField(max_digits=20)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'})
        f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'}))
        self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" required>') 
Example #6
Source File: test_floatfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_floatfield_widget_attrs(self):
        f = FloatField(widget=NumberInput(attrs={'step': 0.01, 'max': 1.0, 'min': 0.0}))
        self.assertWidgetRendersTo(
            f,
            '<input step="0.01" name="f" min="0.0" max="1.0" type="number" id="id_f" required>',
        ) 
Example #7
Source File: test_decimalfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_decimalfield_widget_attrs(self):
        f = DecimalField(max_digits=6, decimal_places=2)
        self.assertEqual(f.widget_attrs(Widget()), {})
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '0.01'})
        f = DecimalField(max_digits=10, decimal_places=0)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1'})
        f = DecimalField(max_digits=19, decimal_places=19)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'})
        f = DecimalField(max_digits=20)
        self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'})
        f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'}))
        self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" required>') 
Example #8
Source File: forms.py    From djangocms-forms with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def prepare_number(self, field):
        field_attrs = field.build_field_attrs()
        widget_attrs = field.build_widget_attrs()

        field_attrs.update({
            'widget': forms.NumberInput(attrs=widget_attrs)
        })
        return forms.IntegerField(**field_attrs) 
Example #9
Source File: forms.py    From django-polaris with Apache License 2.0 4 votes vote down vote up
def __init__(self, transaction: Transaction, *args, **kwargs):
        # For testing via anchor-validator.herokuapp.com
        test_value = kwargs.pop("test_value", None)
        if not test_value:
            test_value = (
                "103" if transaction.kind == Transaction.KIND.deposit else "100"
            )

        super().__init__(*args, **kwargs)

        self.transaction = transaction
        self.asset = transaction.asset
        self.decimal_places = self.asset.significant_decimals
        if transaction.kind == Transaction.KIND.deposit:
            self.min_amount = round(self.asset.deposit_min_amount, self.decimal_places)
            self.max_amount = round(self.asset.deposit_max_amount, self.decimal_places)
            self.min_default = Asset._meta.get_field("deposit_min_amount").default
            self.max_default = Asset._meta.get_field("deposit_max_amount").default
        else:
            self.min_amount = round(
                self.asset.withdrawal_min_amount, self.decimal_places
            )
            self.max_amount = round(
                self.asset.withdrawal_max_amount, self.decimal_places
            )
            self.min_default = Asset._meta.get_field("withdrawal_min_amount").default
            self.max_default = Asset._meta.get_field("withdrawal_max_amount").default

        # Re-initialize the 'amount' field now that we have all the parameters necessary
        self.fields["amount"].__init__(
            widget=forms.NumberInput(
                attrs={
                    "class": "input",
                    "inputmode": "decimal",
                    "symbol": self.asset.symbol,
                    "test-value": test_value,
                }
            ),
            min_value=self.min_amount,
            max_value=self.max_amount,
            decimal_places=self.decimal_places,
            label=_("Amount"),
            localize=True,
        )

        limit_str = ""
        if self.min_amount > self.min_default and self.max_amount < self.max_default:
            limit_str = f"({intcomma(self.min_amount)} - {intcomma(self.max_amount)})"
        elif self.min_amount > self.min_default:
            limit_str = _("(minimum: %s)") % intcomma(self.min_amount)
        elif self.max_amount < self.max_default:
            limit_str = _("(maximum: %s)") % intcomma(self.max_amount)

        if limit_str:
            self.fields["amount"].label += " " + limit_str