Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
dd9a23d5
Kaydet (Commit)
dd9a23d5
authored
Tem 04, 2014
tarafından
Claude Paroz
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #22950 -- Eased markup customization for choice field rendering
Thanks Patrick Robertson for the report.
üst
92090492
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
48 deletions
+68
-48
widgets.py
django/forms/widgets.py
+10
-7
test_widgets.py
tests/forms_tests/tests/test_widgets.py
+58
-41
No files found.
django/forms/widgets.py
Dosyayı görüntüle @
dd9a23d5
...
@@ -643,6 +643,8 @@ class ChoiceFieldRenderer(object):
...
@@ -643,6 +643,8 @@ class ChoiceFieldRenderer(object):
"""
"""
choice_input_class
=
None
choice_input_class
=
None
outer_html
=
'<ul{id_attr}>{content}</ul>'
inner_html
=
'<li>{choice_value}{sub_widgets}</li>'
def
__init__
(
self
,
name
,
value
,
attrs
,
choices
):
def
__init__
(
self
,
name
,
value
,
attrs
,
choices
):
self
.
name
=
name
self
.
name
=
name
...
@@ -664,8 +666,7 @@ class ChoiceFieldRenderer(object):
...
@@ -664,8 +666,7 @@ class ChoiceFieldRenderer(object):
item in the list will get an id of `$id_$i`).
item in the list will get an id of `$id_$i`).
"""
"""
id_
=
self
.
attrs
.
get
(
'id'
,
None
)
id_
=
self
.
attrs
.
get
(
'id'
,
None
)
start_tag
=
format_html
(
'<ul id="{0}">'
,
id_
)
if
id_
else
'<ul>'
output
=
[]
output
=
[
start_tag
]
for
i
,
choice
in
enumerate
(
self
.
choices
):
for
i
,
choice
in
enumerate
(
self
.
choices
):
choice_value
,
choice_label
=
choice
choice_value
,
choice_label
=
choice
if
isinstance
(
choice_label
,
(
tuple
,
list
)):
if
isinstance
(
choice_label
,
(
tuple
,
list
)):
...
@@ -677,14 +678,16 @@ class ChoiceFieldRenderer(object):
...
@@ -677,14 +678,16 @@ class ChoiceFieldRenderer(object):
attrs
=
attrs_plus
,
attrs
=
attrs_plus
,
choices
=
choice_label
)
choices
=
choice_label
)
sub_ul_renderer
.
choice_input_class
=
self
.
choice_input_class
sub_ul_renderer
.
choice_input_class
=
self
.
choice_input_class
output
.
append
(
format_html
(
'<li>{0}{1}</li>'
,
choice_value
,
output
.
append
(
format_html
(
self
.
inner_html
,
choice_value
=
choice_value
,
sub_ul_renderer
.
render
()))
sub_
widgets
=
sub_
ul_renderer
.
render
()))
else
:
else
:
w
=
self
.
choice_input_class
(
self
.
name
,
self
.
value
,
w
=
self
.
choice_input_class
(
self
.
name
,
self
.
value
,
self
.
attrs
.
copy
(),
choice
,
i
)
self
.
attrs
.
copy
(),
choice
,
i
)
output
.
append
(
format_html
(
'<li>{0}</li>'
,
force_text
(
w
)))
output
.
append
(
format_html
(
self
.
inner_html
,
output
.
append
(
'</ul>'
)
choice_value
=
force_text
(
w
),
sub_widgets
=
''
))
return
mark_safe
(
'
\n
'
.
join
(
output
))
return
format_html
(
self
.
outer_html
,
id_attr
=
format_html
(
' id="{0}"'
,
id_
)
if
id_
else
''
,
content
=
mark_safe
(
'
\n
'
.
join
(
output
)))
class
RadioFieldRenderer
(
ChoiceFieldRenderer
):
class
RadioFieldRenderer
(
ChoiceFieldRenderer
):
...
...
tests/forms_tests/tests/test_widgets.py
Dosyayı görüntüle @
dd9a23d5
...
@@ -17,7 +17,7 @@ from django.forms import (
...
@@ -17,7 +17,7 @@ from django.forms import (
)
)
from
django.forms.widgets
import
RadioFieldRenderer
from
django.forms.widgets
import
RadioFieldRenderer
from
django.utils.deprecation
import
RemovedInDjango19Warning
from
django.utils.deprecation
import
RemovedInDjango19Warning
from
django.utils.safestring
import
mark_safe
from
django.utils.safestring
import
mark_safe
,
SafeData
from
django.utils
import
six
from
django.utils
import
six
from
django.utils.translation
import
activate
,
deactivate
,
override
from
django.utils.translation
import
activate
,
deactivate
,
override
from
django.test
import
TestCase
,
override_settings
from
django.test
import
TestCase
,
override_settings
...
@@ -615,6 +615,36 @@ class FormsWidgetTestCase(TestCase):
...
@@ -615,6 +615,36 @@ class FormsWidgetTestCase(TestCase):
<li><label><input type="radio" name="num" value="5" /> 5</label></li>
<li><label><input type="radio" name="num" value="5" /> 5</label></li>
</ul>"""
)
</ul>"""
)
# Choices are escaped correctly
w
=
RadioSelect
()
self
.
assertHTMLEqual
(
w
.
render
(
'escape'
,
None
,
choices
=
((
'bad'
,
'you & me'
),
(
'good'
,
mark_safe
(
'you > me'
)))),
"""<ul>
<li><label><input type="radio" name="escape" value="bad" /> you & me</label></li>
<li><label><input type="radio" name="escape" value="good" /> you > me</label></li>
</ul>"""
)
# Unicode choices are correctly rendered as HTML
w
=
RadioSelect
()
self
.
assertHTMLEqual
(
six
.
text_type
(
w
.
render
(
'email'
,
'ŠĐĆŽćžšđ'
,
choices
=
[(
'ŠĐĆŽćžšđ'
,
'ŠĐabcĆŽćžšđ'
),
(
'ćžšđ'
,
'abcćžšđ'
)])),
'<ul>
\n
<li><label><input checked="checked" type="radio" name="email" value="
\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111
" />
\u0160\u0110
abc
\u0106\u017d\u0107\u017e\u0161\u0111
</label></li>
\n
<li><label><input type="radio" name="email" value="
\u0107\u017e\u0161\u0111
" /> abc
\u0107\u017e\u0161\u0111
</label></li>
\n
</ul>'
)
# Attributes provided at instantiation are passed to the constituent inputs
w
=
RadioSelect
(
attrs
=
{
'id'
:
'foo'
})
self
.
assertHTMLEqual
(
w
.
render
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
))),
"""<ul id="foo">
<li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
<li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
<li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
</ul>"""
)
# Attributes provided at render-time are passed to the constituent inputs
w
=
RadioSelect
()
self
.
assertHTMLEqual
(
w
.
render
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
)),
attrs
=
{
'id'
:
'bar'
}),
"""<ul id="bar">
<li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
<li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
<li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
</ul>"""
)
def
test_radiofieldrenderer
(
self
):
# RadioSelect uses a RadioFieldRenderer to render the individual radio inputs.
# RadioSelect uses a RadioFieldRenderer to render the individual radio inputs.
# You can manipulate that object directly to customize the way the RadioSelect
# You can manipulate that object directly to customize the way the RadioSelect
# is rendered.
# is rendered.
...
@@ -648,6 +678,18 @@ beatle J P Paul False
...
@@ -648,6 +678,18 @@ beatle J P Paul False
beatle J G George False
beatle J G George False
beatle J R Ringo False"""
)
beatle J R Ringo False"""
)
# A RadioFieldRenderer object also allows index access to individual RadioChoiceInput
w
=
RadioSelect
()
r
=
w
.
get_renderer
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
)))
self
.
assertHTMLEqual
(
str
(
r
[
1
]),
'<label><input type="radio" name="beatle" value="P" /> Paul</label>'
)
self
.
assertHTMLEqual
(
str
(
r
[
0
]),
'<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label>'
)
self
.
assertTrue
(
r
[
0
]
.
is_checked
())
self
.
assertFalse
(
r
[
1
]
.
is_checked
())
self
.
assertEqual
((
r
[
1
]
.
name
,
r
[
1
]
.
value
,
r
[
1
]
.
choice_value
,
r
[
1
]
.
choice_label
),
(
'beatle'
,
'J'
,
'P'
,
'Paul'
))
with
self
.
assertRaises
(
IndexError
):
r
[
10
]
# You can create your own custom renderers for RadioSelect to use.
# You can create your own custom renderers for RadioSelect to use.
class
MyRenderer
(
RadioFieldRenderer
):
class
MyRenderer
(
RadioFieldRenderer
):
def
render
(
self
):
def
render
(
self
):
...
@@ -667,46 +709,21 @@ beatle J R Ringo False""")
...
@@ -667,46 +709,21 @@ beatle J R Ringo False""")
<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
<label><input type="radio" name="beatle" value="R" /> Ringo</label>"""
)
<label><input type="radio" name="beatle" value="R" /> Ringo</label>"""
)
# A RadioFieldRenderer object also allows index access to individual RadioChoiceInput
# You can customize rendering with outer_html/inner_html renderer variables (#22950)
w
=
RadioSelect
()
class
MyRenderer
(
RadioFieldRenderer
):
r
=
w
.
get_renderer
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
)))
outer_html
=
str
(
'<div{id_attr}>{content}</div>'
)
# str is just to test some Python 2 issue with bytestrings
self
.
assertHTMLEqual
(
str
(
r
[
1
]),
'<label><input type="radio" name="beatle" value="P" /> Paul</label>'
)
inner_html
=
'<p>{choice_value}{sub_widgets}</p>'
self
.
assertHTMLEqual
(
str
(
r
[
0
]),
'<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label>'
)
w
=
RadioSelect
(
renderer
=
MyRenderer
)
self
.
assertTrue
(
r
[
0
]
.
is_checked
())
output
=
w
.
render
(
'beatle'
,
'J'
,
self
.
assertFalse
(
r
[
1
]
.
is_checked
())
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
)),
self
.
assertEqual
((
r
[
1
]
.
name
,
r
[
1
]
.
value
,
r
[
1
]
.
choice_value
,
r
[
1
]
.
choice_label
),
(
'beatle'
,
'J'
,
'P'
,
'Paul'
))
attrs
=
{
'id'
:
'bar'
})
self
.
assertIsInstance
(
output
,
SafeData
)
with
self
.
assertRaises
(
IndexError
):
self
.
assertHTMLEqual
(
output
,
"""<div id="bar">
r
[
10
]
<p><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></p>
<p><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></p>
# Choices are escaped correctly
<p><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></p>
w
=
RadioSelect
()
<p><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></p>
self
.
assertHTMLEqual
(
w
.
render
(
'escape'
,
None
,
choices
=
((
'bad'
,
'you & me'
),
(
'good'
,
mark_safe
(
'you > me'
)))),
"""<ul>
</div>"""
)
<li><label><input type="radio" name="escape" value="bad" /> you & me</label></li>
<li><label><input type="radio" name="escape" value="good" /> you > me</label></li>
</ul>"""
)
# Unicode choices are correctly rendered as HTML
w
=
RadioSelect
()
self
.
assertHTMLEqual
(
six
.
text_type
(
w
.
render
(
'email'
,
'ŠĐĆŽćžšđ'
,
choices
=
[(
'ŠĐĆŽćžšđ'
,
'ŠĐabcĆŽćžšđ'
),
(
'ćžšđ'
,
'abcćžšđ'
)])),
'<ul>
\n
<li><label><input checked="checked" type="radio" name="email" value="
\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111
" />
\u0160\u0110
abc
\u0106\u017d\u0107\u017e\u0161\u0111
</label></li>
\n
<li><label><input type="radio" name="email" value="
\u0107\u017e\u0161\u0111
" /> abc
\u0107\u017e\u0161\u0111
</label></li>
\n
</ul>'
)
# Attributes provided at instantiation are passed to the constituent inputs
w
=
RadioSelect
(
attrs
=
{
'id'
:
'foo'
})
self
.
assertHTMLEqual
(
w
.
render
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
))),
"""<ul id="foo">
<li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
<li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
<li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
</ul>"""
)
# Attributes provided at render-time are passed to the constituent inputs
w
=
RadioSelect
()
self
.
assertHTMLEqual
(
w
.
render
(
'beatle'
,
'J'
,
choices
=
((
'J'
,
'John'
),
(
'P'
,
'Paul'
),
(
'G'
,
'George'
),
(
'R'
,
'Ringo'
)),
attrs
=
{
'id'
:
'bar'
}),
"""<ul id="bar">
<li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
<li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
<li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
</ul>"""
)
def
test_nested_choices
(
self
):
def
test_nested_choices
(
self
):
# Choices can be nested for radio buttons:
# Choices can be nested for radio buttons:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment