Opened 15 years ago

Closed 15 years ago

Last modified 12 years ago

#8194 closed (fixed)

Admin form keyboard focus does not respect fields or fieldsets options

Reported by: mdhancher Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: newforms-admin
Cc: ciantic@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When you re-order the fields in an admin add form using the fields or fieldsets options in your ModelAdmin class, the initial keyboard focus in the admin add/change form does not respect it.

The keyboard focus is set by a JavaScript call to document.getElementById("id_foo").focus(), where the ID is specified in the change_form.html template as {{adminform.first_field.auto_id}}. So an underlying issue is that AdminForm.first_field() (in contrib/admin/options.py) does not respect the re-ordering. I am experiencing this using the current Subversion HEAD, revision 8272.

For example, consider this simple model in models.py:

class MyModel(models.Model):
    foo = models.CharField(max_length=255)
    bar = models.CharField(max_length=255)

and this simple admin configuration in admin.py:

class MyModelAdmin(admin.ModelAdmin):
    fields = ('bar', 'foo')

admin.site.register(MyModel,MyModelAdmin)

Then try to add a new MyModel object using the admin interface, and notice that they keyboard focus starts out at the bottom rather than the top of the form.

Attachments (4)

8298.patch (983 bytes) - added by Fred Bartle <fredbartle@…> 15 years ago.
sets focus to first field in admin
8298.2.patch (982 bytes) - added by Fred Bartle <fredbartle@…> 15 years ago.
8194_against_8630.diff (982 bytes) - added by Fred Bartle 15 years ago.
8194_against_8752.diff (441 bytes) - added by Fred Bartle 15 years ago.
Allows access to BoundField

Download all attachments as: .zip

Change History (13)

comment:1 Changed 15 years ago by Brian Rosner

milestone: 1.0
Triage Stage: UnreviewedAccepted

Changed 15 years ago by Fred Bartle <fredbartle@…>

Attachment: 8298.patch added

sets focus to first field in admin

comment:2 Changed 15 years ago by Fred Bartle <fredbartle@…>

Added patch that respects fields or fieldsets specified in ModelAdmin. Required access to forms.BoundField.

Changed 15 years ago by Fred Bartle <fredbartle@…>

Attachment: 8298.2.patch added

comment:3 Changed 15 years ago by Fred Bartle <fredbartle@…>

Fixed a typo in the first patch.

comment:4 Changed 15 years ago by Jari Pennanen

Cc: ciantic@… added
Keywords: newforms-admin added

comment:5 Changed 15 years ago by Fred Bartle

Has patch: set

Suppose I should check this box!

Changed 15 years ago by Fred Bartle

Attachment: 8194_against_8630.diff added

comment:6 Changed 15 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [8681]) Fixed #8194: AdminForm.first_field now correctly returns the first field when using custom field orders.

comment:7 Changed 15 years ago by Fred Bartle

Patch needs improvement: set
Resolution: fixed
Status: closedreopened

I'm reopening because forms.BoundField is not accessible for this patch to work. As of r8681, if admin fields or fieldsets are specified, the following html is created from the template:

<script type="text/javascript">document.getElementById("").focus();</script>

Using a try/except block around L25 in django/contrib/admin/helpers.py shows an AttributeError being raised.

'module' object has no attribute 'BoundField'

Just a side question here, I'm listed under AUTHORS as 'hambaloney' for another ticket I was involved with, is it possible to change that to my name?

Changed 15 years ago by Fred Bartle

Attachment: 8194_against_8752.diff added

Allows access to BoundField

comment:8 Changed 15 years ago by Jacob

Resolution: fixed
Status: reopenedclosed

(In [8774]) Fixed #8194 (again): correctly focus on the first declared field in the admin. Thanks to fredbartle for catching my silly mistake the first time 'round.

comment:9 Changed 12 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top