Login

Django Number Input

Author:
silent1mezzo
Posted:
February 8, 2011
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months 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.