adding html5 field attributes to a model form

86 views
Skip to first unread message

Lee Hinde

unread,
Apr 9, 2012, 9:14:08 PM4/9/12
to django...@googlegroups.com
I'm trying to edit the field type in a form that is using the html5 doctype. I've tried two ways:

class AddressForm(ModelForm):
    contact_email = forms.TextInput(attrs={'type': 'email'})

and 

class AddressForm(ModelForm):
    def __init__(self, *args, **kwargs):
            super(AddressForm, self).__init__(*args, **kwargs)
            self.fields['contact_email'].widget.attrs["type"] = 'email'

Neither works. 

Am I looking in the wrong place?


creecode

unread,
Apr 9, 2012, 9:53:40 PM4/9/12
to django...@googlegroups.com
Hello Lee,


On Monday, April 9, 2012 6:14:08 PM UTC-7, Lee Hinde wrote:

I'm trying to edit the field type in a form that is using the html5 doctype. I've tried two ways:

Neither works. 

Am I looking in the wrong place?

I use both those techniques and they work great for me.  I've use them on many forms for a project I'm working on now.  Some examples from my code...

    brand_name = forms.CharField ( label = _( 'Brand Name' ), max_length = 128,
        widget = forms.TextInput ( attrs = { 'class' : 'span6', 'placeholder' :
        'enter brand name', } ) )

...and...

    def __init__ ( self, *args, **kwargs ):
   
        super ( AmountPerServing1Form, self ).__init__ ( *args, **kwargs )
       
        for key in self.fields:
       
            field = self.fields [ key ]
           
            field.widget.attrs [ 'class' ] = 'span7'

I don't see anything wrong with your syntax.  Have you tried the usual things?  Make sure you save the file your're editing, restart the server, etc?  And still the form didn't show up with your changes?

Could there be some browser or other caching going on between you and the server?

Are you sub-classing those forms for using them directly?

Toodle-looooooooooooo................
creecode

Lee Hinde

unread,
Apr 9, 2012, 10:17:56 PM4/9/12
to django...@googlegroups.com
On Apr 9, 2012, at 6:53 PM, creecode wrote:

Hello Lee,

On Monday, April 9, 2012 6:14:08 PM UTC-7, Lee Hinde wrote:

I'm trying to edit the field type in a form that is using the html5 doctype. I've tried two ways:

Neither works. 

Am I looking in the wrong place?

I use both those techniques and they work great for me.  I've use them on many forms for a project I'm working on now.  Some examples from my code...

    brand_name = forms.CharField ( label = _( 'Brand Name' ), max_length = 128,
        widget = forms.TextInput ( attrs = { 'class' : 'span6', 'placeholder' :
        'enter brand name', } ) )

...and...

    def __init__ ( self, *args, **kwargs ):
   
        super ( AmountPerServing1Form, self ).__init__ ( *args, **kwargs )
       
        for key in self.fields:
       
            field = self.fields [ key ]
           
            field.widget.attrs [ 'class' ] = 'span7'


Thanks for the response, Creecode,

I've used that for adding class attributes just fine, but in this case I'm trying to change the field 'type' and that's what doesn't seem to work.

creecode

unread,
Apr 9, 2012, 10:32:54 PM4/9/12
to django...@googlegroups.com
Ah I see.  I haven't looked deeper in the code I'd guess the type is being set after you set it deeper in the machinery based on field class?  Perhaps a custom field could accomplish your goal?


On Monday, April 9, 2012 7:17:56 PM UTC-7, Lee Hinde wrote:

Thanks for the response, Creecode,

I've used that for adding class attributes just fine, but in this case I'm trying to change the field 'type' and that's what doesn't seem to work.

Toodle-loooooooooooo.....
creecode

Lee Hinde

unread,
Apr 9, 2012, 11:37:16 PM4/9/12
to django...@googlegroups.com
Thanks. I'll check that out.

BlackKnight

unread,
Apr 10, 2012, 1:53:20 AM4/10/12
to Django users
Custom widget would be enough


in your forms.py

from django.forms import Input

class EmailInput(Input):
input_type = 'email'

class AddressForm(ModelForm):
contact_email = forms.CharField(widget=EmailInput)
Reply all
Reply to author
Forward
0 new messages