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 9 years, 4 months ago
  2. Generic model filter from request GET data by genbit 5 years, 1 month ago
  3. Radio widget with labels after inputs by avsd 4 years, 1 month ago
  4. Bitwise operator queryset filter by [email protected] 6 years, 3 months ago
  5. Django csrf_token Template Tag Fix by Reustle 6 years, 1 month ago

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.