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

Comments

bmartinek (on February 24, 2011):
<p>Looks good, I been trying to use this but have had no luck. :-( In my forms.py I have:</p> <p>from django.forms.extras.widgets import *</p> <p>from django.forms.widgets import TextInput (also tried .extras)</p> <p>wine_year_zip = forms.IntegerField( label=u'Year:', widgets = (widget=(NumberInput(attrs={'size': 3} )</p> <p>and</p> <p>year = forms.IntegerField( label=u'Year:', 'number_field': NumberInput(attrs={'min': '0', 'max': '10', 'step': '1'})</p> <p>)</p> <p>Both have resulted in syntax errors. </p> <p>Suggestions?</p>

#

Please login first before commenting.