#10573 closed (fixed)
Bug when auto-focusing on field with MultiWidget in admin site
| Reported by: | rduffield | Owned by: | Colin Copeland |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | ales.zoulek@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The admin site JavaScript, when creating a new instance of a model with a DateTimeField, attempts to focus on an element that does not exist. Take this basic example:
class Reservation(models.Model):
start_date = models.DateTimeField(blank=False, null=False)
This results in the use of the AdminSplitDateTime widget. The two respective HTML fields have IDs of id_start_date_0 and id_start_date_1. However, the JavaScript that is generated is:
document.getElementById("id_start_date").focus();
This fails, due to there not being an element on the page with an ID of id_start_date.
I tracked this down to the _auto_id method within the BoundField class. Creating the auto_id depends on the field's html_name; in the case outlined above, both fields have html_names of start_date. However, the rendering of a MultiWidget is such that the IDs are generated differently than normal widgets.
My naive patch introduces a new property on a BoundField called focus_id. The focus_id returns auto_id as before for all widgets that are not MultiWidgets. Otherwise, it returns the first element within a MultiWidget set. The admin change_form.html template now also takes advantage of this field. I did not want to change any logic within auto_id itself; this seemed a little more dangerous. :-)
Attachments (8)
Change History (26)
by , 17 years ago
| Attachment: | admin.diff added |
|---|
by , 17 years ago
| Attachment: | tests.diff added |
|---|
comment:1 by , 17 years ago
follow-up: 3 comment:2 by , 17 years ago
Can we instead have the javascript find the first form element automatically and focus that?
comment:3 by , 17 years ago
That certainly seems like a great alternative to me; I was simply trying to maintain what I figured was the convention.
comment:4 by , 17 years ago
| milestone: | → 1.1 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:5 by , 17 years ago
| Cc: | added |
|---|
comment:6 by , 17 years ago
I added a patch. Focusing first form field could not be as trivial at it seems. But it should work fine.
comment:7 by , 17 years ago
| Owner: | changed from to |
|---|
by , 17 years ago
| Attachment: | focus_first.2.patch added |
|---|
comment:8 by , 17 years ago
updated the patch to first try it the old way and if it fails to fall back to for-loop.
comment:9 by , 17 years ago
| milestone: | 1.1 → 1.2 |
|---|
Pushing to 1.2: this is a UI issue which is annoying but doesn't impact usability.
comment:10 by , 16 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
by , 16 years ago
| Attachment: | 10573_r12827_with_fields.diff added |
|---|
alternative patch if #12627 is closed
comment:11 by , 16 years ago
Tested in:
- Mac: Safari 4.0.5, Firefox 3.6, Chrome 5.0.307.11 beta
- Windows: Internet Explorer 6.0.2800.1106
comment:12 by , 16 years ago
| milestone: | 1.2 → 1.3 |
|---|
This is another ticket that's not really release-critical; pushing off the 1.2 milestone, and we'll likely hit it in 1.3 (which is being scheduled to focus on these sorts of minor-annoyance issues).
comment:13 by , 15 years ago
comment:14 by , 15 years ago
I've just attached a patch that, just like rduffield's patch, proposes solving this on the Python side by adding a property to django.forms.BoundField.
Difference is that it uses a strategy similar to the label_tag() method to make sure it returns a form ID usable for e.e. auto-focusing the field. The new property name is stable_field, for lack of a better name, can be changed if native English speakers deem necessary.
by , 15 years ago
| Attachment: | 10573.2.diff added |
|---|
comment:15 by , 15 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Ramiro, I like the approach in your patch. I've just updated it with a test for when the first field only has a single widget. I've also renamed the stable_id property to id_for_label, which may be a bit more versatile. Thanks!
I have attached a test that reproduces this bug.
Note that I should have been more clear; this bug is only evident if the
DateTimefield is of course the first field rendered in the admin form.