Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interesting question. HTML 4.01 also prohibits <code>accesskey</code> in a <code>select</code>.</p> <p>I believe the Short Answer is: Not in standard Django.</p> <p><strong>Much longer answer:</strong> I looked at the code in <code>django/forms/fields.py</code> and <code>.../widgets.py</code> and the label is handled strictly as a string (forced to <code>smart_unicode()</code>). Four possible solutions come to mind, the first three are not pretty:</p> <ol> <li><p>Ignore the validation failure. I hate doing this, but sometimes it's a necessary kludge. Most browsers are much looser than the DTDs in what they allow. If you can get the accesskey to work even when it's technically in the wrong place, that might be the simplest way to go.</p></li> <li><p>Catch the output of the template and do some sort of ugly search-and-replace. (Blech!)</p></li> <li><p>Add new functionality to the widgets/forms code by <a href="http://en.wikipedia.org/wiki/Monkey_patch" rel="nofollow noreferrer">MonkeyPatching</a> it. MonkeyPatch <code>django.forms.fields.Field</code> to catch and save a new arg (<code>label_attrs</code>?). MonkeyPatch the <code>label_tag()</code> method of <code>forms.forms.BoundField</code> to deal with the new <code>widget.label_attrs</code> value.</p> <p>I'm deliberately not going to give more details on this. If you understand the code well enough to MonkeyPatch it, then you are smart enough to know the dangers inherent in doing this.</p></li> <li><p>Make the same functional changes as #3, but do it as a submitted patch to the Django code base. This is the best long-term answer for everyone, but it's also the most work for you.</p></li> </ol> <p><strong>Update:</strong> Yoni Samlan's link to a custom filter (<a href="http://www.djangosnippets.org/snippets/693/" rel="nofollow noreferrer">http://www.djangosnippets.org/snippets/693/</a>) works if you are generating the <code>&lt;label&gt;</code> tag yourself. My answers are directed toward still using the full power of Forms but trying to tweak the resultant <code>&lt;label&gt;</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload