Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17596 -- Stopped the AdminField class from double quoting its …

…label. Thanks, guettli and claudep.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17431 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bb6921ce888cfc981c4df8d5f079b0b7a4121ff6 1 parent 9c045d0
Jannis Leidel authored February 04, 2012
6  django/contrib/admin/helpers.py
@@ -115,17 +115,17 @@ def __init__(self, form, field, is_first):
115 115
 
116 116
     def label_tag(self):
117 117
         classes = []
  118
+        contents = conditional_escape(force_unicode(self.field.label))
118 119
         if self.is_checkbox:
119 120
             classes.append(u'vCheckboxLabel')
120  
-            contents = force_unicode(escape(self.field.label))
121 121
         else:
122  
-            contents = force_unicode(escape(self.field.label)) + u':'
  122
+            contents += u':'
123 123
         if self.field.field.required:
124 124
             classes.append(u'required')
125 125
         if not self.is_first:
126 126
             classes.append(u'inline')
127 127
         attrs = classes and {'class': u' '.join(classes)} or {}
128  
-        return self.field.label_tag(contents=contents, attrs=attrs)
  128
+        return self.field.label_tag(contents=mark_safe(contents), attrs=attrs)
129 129
 
130 130
     def errors(self):
131 131
         return mark_safe(self.field.errors.as_ul())
26  tests/regressiontests/admin_util/tests.py
@@ -4,14 +4,17 @@
4 4
 
5 5
 from django.conf import settings
6 6
 from django.contrib import admin
  7
+from django.contrib.admin import helpers
7 8
 from django.contrib.admin.util import (display_for_field, label_for_field,
8 9
     lookup_field, NestedObjects)
9 10
 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
10 11
 from django.contrib.sites.models import Site
11 12
 from django.db import models, DEFAULT_DB_ALIAS
  13
+from django import forms
12 14
 from django.test import TestCase
13 15
 from django.utils import unittest
14 16
 from django.utils.formats import localize
  17
+from django.utils.safestring import mark_safe
15 18
 
16 19
 from .models import Article, Count, Event, Location
17 20
 
@@ -258,3 +261,26 @@ def test_logentry_unicode(self):
258 261
         self.assertTrue(
259 262
             unicode(log_entry).startswith('Deleted ')
260 263
         )
  264
+
  265
+    def test_safestring_in_field_label(self):
  266
+        # safestring should not be escaped
  267
+        class MyForm(forms.Form):
  268
+            text = forms.CharField(label=mark_safe('<i>text</i>'))
  269
+            cb   = forms.BooleanField(label=mark_safe('<i>cb</i>'))
  270
+
  271
+        form = MyForm()
  272
+        self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
  273
+                         '<label for="id_text" class="required inline"><i>text</i>:</label>')
  274
+        self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
  275
+                         '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>')
  276
+
  277
+        # normal strings needs to be escaped
  278
+        class MyForm(forms.Form):
  279
+            text = forms.CharField(label='&text')
  280
+            cb   = forms.BooleanField(label='&cb')
  281
+
  282
+        form = MyForm()
  283
+        self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
  284
+                         '<label for="id_text" class="required inline">&amp;text:</label>')
  285
+        self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
  286
+                         '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb</label>')

0 notes on commit bb6921c

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