Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #11277 -- Hid labels of fields with HiddenInput widget in admin…

… forms.

Thanks bartTC for the report.
  • Loading branch information...
commit dc3d2ac98c1bcfad74d3e9523caf07e7e9fb15aa 1 parent bcd4c3f
Christopher Medrela authored timgraham committed
View
3  django/contrib/admin/helpers.py
@@ -84,6 +84,9 @@ def __init__(self, form, field, readonly_fields=None, model_admin=None):
self.fields = [field]
else:
self.fields = field
+ self.has_visible_field = not all(field in self.form.fields and
+ self.form.fields[field].widget.is_hidden
+ for field in self.fields)
self.model_admin = model_admin
if readonly_fields is None:
readonly_fields = ()
View
4 django/contrib/admin/static/admin/css/forms.css
@@ -18,6 +18,10 @@ form .form-row p {
font-size: 11px;
}
+.hidden {
+ display: none;
+}
+
/* FORM LABELS */
form h4 {
View
4 django/contrib/admin/templates/admin/includes/fieldset.html
@@ -4,10 +4,10 @@
<div class="description">{{ fieldset.description|safe }}</div>
{% endif %}
{% for line in fieldset %}
- <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
+ <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
{% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
{% for field in line %}
- <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
+ <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
{% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
View
36 tests/admin_views/admin.py
@@ -30,7 +30,8 @@
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
RelatedPrepopulated, UndeletableObject, UnchangeableObject, UserMessenger, Simple, Choice,
- ShortMessage, Telegram, FilteredManager)
+ ShortMessage, Telegram, FilteredManager, EmptyModelHidden,
+ EmptyModelVisible, EmptyModelMixin)
def callable_year(dt_value):
@@ -709,6 +710,36 @@ class ChoiceList(admin.ModelAdmin):
fields = ['choice']
+# Tests for ticket 11277 ----------------------------------
+
+class FormWithoutHiddenField(forms.ModelForm):
+ first = forms.CharField()
+ second = forms.CharField()
+
+class FormWithoutVisibleField(forms.ModelForm):
+ first = forms.CharField(widget=forms.HiddenInput)
+ second = forms.CharField(widget=forms.HiddenInput)
+
+class FormWithVisibleAndHiddenField(forms.ModelForm):
+ first = forms.CharField(widget=forms.HiddenInput)
+ second = forms.CharField()
+
+class EmptyModelVisibleAdmin(admin.ModelAdmin):
+ form = FormWithoutHiddenField
+ fieldsets = (
+ (None, {
+ 'fields':(('first', 'second'),),
+ }),
+ )
+
+class EmptyModelHiddenAdmin(admin.ModelAdmin):
+ form = FormWithoutVisibleField
+ fieldsets = EmptyModelVisibleAdmin.fieldsets
+
+class EmptyModelMixinAdmin(admin.ModelAdmin):
+ form = FormWithVisibleAndHiddenField
+ fieldsets = EmptyModelVisibleAdmin.fieldsets
+
site = admin.AdminSite(name="admin")
site.register(Article, ArticleAdmin)
site.register(CustomArticle, CustomArticleAdmin)
@@ -790,6 +821,9 @@ class ChoiceList(admin.ModelAdmin):
site.register(Simple, AttributeErrorRaisingAdmin)
site.register(UserMessenger, MessageTestingAdmin)
site.register(Choice, ChoiceList)
+site.register(EmptyModelHidden, EmptyModelHiddenAdmin)
+site.register(EmptyModelVisible, EmptyModelVisibleAdmin)
+site.register(EmptyModelMixin, EmptyModelMixinAdmin)
# Register core models we need in our tests
from django.contrib.auth.models import User, Group
View
9 tests/admin_views/models.py
@@ -704,3 +704,12 @@ def __str__(self):
pk_gt_1 = _Manager()
objects = models.Manager()
+
+class EmptyModelVisible(models.Model):
+ """ See ticket #11277. """
+
+class EmptyModelHidden(models.Model):
+ """ See ticket #11277. """
+
+class EmptyModelMixin(models.Model):
+ """ See ticket #11277. """
View
42 tests/admin_views/tests.py
@@ -4571,5 +4571,47 @@ def test_delete_view(self):
response = self.client.post(self.get_delete_url(), {'post': 'yes'})
self.assertRedirects(response, self.get_changelist_url())
+
class NamespacedAdminKeepChangeListFiltersTests(AdminKeepChangeListFiltersTests):
admin_site = site2
+
+
+@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
+class TestLabelVisibility(TestCase):
+ """ #11277 -Labels of hidden fields in admin were not hidden. """
+
+ urls = "admin_views.urls"
+ fixtures = ['admin-views-users.xml']
+
+ def setUp(self):
+ self.client.login(username='super', password='secret')
+
+ def test_all_fields_visible(self):
+ response = self.client.get('/test_admin/admin/admin_views/emptymodelvisible/add/')
+ self.assert_fieldline_visible(response)
+ self.assert_field_visible(response, 'first')
+ self.assert_field_visible(response, 'second')
+
+ def test_all_fields_hidden(self):
+ response = self.client.get('/test_admin/admin/admin_views/emptymodelhidden/add/')
+ self.assert_fieldline_hidden(response)
+ self.assert_field_hidden(response, 'first')
+ self.assert_field_hidden(response, 'second')
+
+ def test_mixin(self):
+ response = self.client.get('/test_admin/admin/admin_views/emptymodelmixin/add/')
+ self.assert_fieldline_visible(response)
+ self.assert_field_hidden(response, 'first')
+ self.assert_field_visible(response, 'second')
+
+ def assert_field_visible(self, response, field_name):
+ self.assertContains(response, '<div class="field-box field-%s">' % field_name)
+
+ def assert_field_hidden(self, response, field_name):
+ self.assertContains(response, '<div class="field-box field-%s hidden">' % field_name)
+
+ def assert_fieldline_visible(self, response):
+ self.assertContains(response, '<div class="form-row field-first field-second">')
+
+ def assert_fieldline_hidden(self, response):
+ self.assertContains(response, '<div class="form-row hidden')
Please sign in to comment.
Something went wrong with that request. Please try again.