Login

Django Number Input

Author:
silent1mezzo
Posted:
February 8, 2011
Language:
Python
Version:
1.2
Tags:
django django-forms input-types
Score:
1 (after 1 ratings)

This will give you a <input type='number'> field.

This is helpful when you want to use HTML5 for newer browsers. Older browsers will just interpret this as <input type='text'>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.forms.widgets import TextInput

class NumberInput(TextInput):
    input_type = 'number'


#Usage
widgets = (
    'number_field': NumberInput(attrs={'min': '0', 'max': '10', 'step': '1'}),

)

More like this

  1. HttpMethodsMiddleware by hawkeye 8 years, 3 months ago
  2. Generic model filter from request GET data by genbit 4 years ago
  3. Radio widget with labels after inputs by avsd 3 years ago
  4. Bitwise operator queryset filter by hgeerts@osso.nl 5 years, 2 months ago
  5. Django csrf_token Template Tag Fix by Reustle 5 years ago

Comments

bmartinek (on February 24, 2011):

Looks good, I been trying to use this but have had no luck. :-( In my forms.py I have:

from django.forms.extras.widgets import *

from django.forms.widgets import TextInput (also tried .extras)

wine_year_zip = forms.IntegerField( label=u'Year:', widgets = (widget=(NumberInput(attrs={'size': 3} )

and

year = forms.IntegerField( label=u'Year:', 'number_field': NumberInput(attrs={'min': '0', 'max': '10', 'step': '1'})

)

Both have resulted in syntax errors.

Suggestions?

#

Please login first before commenting.