Django:无法在HTML输入元素中添加UPPERCASE属性名称 [英] Django: Unable to add UPPERCASE attribute name in HTML input element
问题描述
我正在尝试在django中使用一个javascript库,它需要一些camelCase中的HTML元素的属性。例如,我有一个带有CharField字段的模型:
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")
< >我的ModelForm在 init 方法中有以下一行:
self.fields['expires'].widget.attrs['SomeAttribute'] = "SomeValue"
之后的render_to_response输出的HTML是这样的:
and after the render_to_response the outputed HTML is like this:
<input id="id_expires" type="text" name="expires" someattribute="SomeValue">
而不是:
<input id="id_expires" type="text" name="expires" SomeAttribute="SomeValue">
我错过了什么?
推荐答案
正如Issac指出的那样,你应该是正确的。负责在django.forms.widgets中渲染上述的Django内部
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))
应该给你正确的attr你正在寻找。当我检查在Firebug中呈现的HTML时,我确实会复制你的问题。看来Firebug降低了属性名称,但是当我做了一个视图源代码时,它显示为SomeAttribute对Firebug中的某个属性(如果这确实是你正在做的))
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 :))
这篇关于Django:无法在HTML输入元素中添加UPPERCASE属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!