0

I'm trying to use a javascript library in django that requires some attributes for HTML elements in camelCase. For example, I've a model with a CharField field like this:

expires = models.DateField("Expiration Date", db_index = False, blank = True, null = True, editable = True, help_text = "Something")

My ModelForm has the following line in the init method:

self.fields['expires'].widget.attrs['SomeAttribute'] = "SomeValue"

and after the render_to_response the outputed HTML is like this:

<input id="id_expires" type="text" name="expires" someattribute="SomeValue">

instead of:

<input id="id_expires" type="text" name="expires" SomeAttribute="SomeValue">

Am I missing something?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Reichert
  • 133
  • 5
  • What is the Javascript library? If it's interacting with the DOM correctly the casing shouldn't matter at all. – Jordan Oct 19 '11 at 04:13
  • Thanks Jordan, I'm using jQuery. I suppose the attribute label is case insensitive, right? – Reichert Oct 22 '11 at 20:19
  • Yes, that's correct. The attribute casing shouldn't matter; last time I checked every browser pulled it back in a case insensitive manner, since HTML and XHTML have different standards about casing anyway. – Jordan Oct 24 '11 at 01:53

2 Answers2

0

I couldn't find anything in the django forms codebase that suggests that it's anything that's django's fault. How are you rendering the form? Please see my shell session for my details.

>>> from django import forms
>>> class F(forms.Form):
...  a = forms.CharField()
... 
>>> f = F()
>>> f.as_p()
u'<p><label for="id_a">A:</label> <input type="text" name="a" id="id_a" /></p>'
>>> f.fields['a'].widget.attrs
{}
>>> f.fields['a'].widget.attrs['dERP'] = 'ddf'
>>> f.as_p()
u'<p><label for="id_a">A:</label> <input id="id_a" type="text" name="a" dERP="ddf" /></p>'
>>> 
Issac Kelly
  • 6,309
  • 6
  • 43
  • 50
0

As Issac points out at the top, what you've should be correct. The Django internals responsible for rendering the above in django.forms.widgets

return mark_safe(u'<input%s />' % flatatt(final_attrs))

should give you the correct attr you're looking for. I did get to replicate your problem when I inspected the HTML rendered in Firebug. It seems that Firebug lowercases the attribute name but when I did a view source code, it did show as SomeAttribute versus someattribute in Firebug (if this is indeed what you're doing :))

Kenny Shen
  • 4,773
  • 3
  • 21
  • 18