Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17074 -- Improved first field detection in admin add form

Thanks Kidwind for the report and Julien Phalip for the initial
patch.
  • Loading branch information...
commit 1c0c879be3a21affec3d5eb2d6b6e4f0a0803aec 1 parent 92a710c
@claudep claudep authored
View
14 django/contrib/admin/helpers.py
@@ -46,20 +46,6 @@ def __iter__(self):
**options
)
- def first_field(self):
- try:
- fieldset_name, fieldset_options = self.fieldsets[0]
- field_name = fieldset_options['fields'][0]
- if not isinstance(field_name, six.string_types):
- field_name = field_name[0]
- return self.form[field_name]
- except (KeyError, IndexError):
- pass
- try:
- return next(iter(self.form))
- except StopIteration:
- return None
-
def _media(self):
media = self.form.media
for fs in self:
View
10 django/contrib/admin/templates/admin/change_form.html
@@ -66,8 +66,14 @@
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
-{% if adminform.first_field and add %}
- <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
+{% if adminform and add %}
+ <script type="text/javascript">
+ (function($) {
+ $(document).ready(function() {
+ $('form#{{ opts.module_name }}_form :input:visible:enabled:first').focus()
+ });
+ })(django.jQuery);
+ </script>
{% endif %}
{# JavaScript for prepopulated fields #}

3 comments on commit 1c0c879

@timgraham
Owner

There are two tests for the old behavior which are failing:

admin_views.tests.AdminJavaScriptTest.testMultiWidgetFirsFieldFocus
admin_views.tests.AdminJavaScriptTest.testSingleWidgetFirsFieldFocus

I guess they could be updated for the new jQuery selector.

@ramiro
Collaborator

Also, these tests have a typo ("Firs") in their names

I suspect that this could the reason they weren't found when fixing this ticket.

And also suspect I'm the one that introduced these tests :-(

@ramiro
Collaborator

Fixed in 41feab3. Converted these tests to use Selenium as actually there is no point in testing presence of JS code that now is hard-coded in the template.

Please sign in to comment.
Something went wrong with that request. Please try again.