Форум сайта python.su
Подскажите, как изменить форму вывода widgets = RadioSelect
Не устраивает <ul> и <li>
Есть сильное желание выводить в ячейки таблицы, а вот как сделать не знаю.
Офлайн
KamberУ виджета RadioSelect в конструктор можно передать именованый параметр - renderer.
Подскажите, как изменить форму вывода widgets = RadioSelect
Не устраивает <ul> и <li>
Есть сильное желание выводить в ячейки таблицы, а вот как сделать не знаю.
class RadioFieldRenderer(StrAndUnicode):
"""
An object used by RadioSelect to enable customization of radio widgets.
"""
def __init__(self, name, value, attrs, choices):
self.name, self.value, self.attrs = name, value, attrs
self.choices = choices
def __iter__(self):
for i, choice in enumerate(self.choices):
yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i)
def __getitem__(self, idx):
choice = self.choices[idx] # Let the IndexError propogate
return RadioInput(self.name, self.value, self.attrs.copy(), choice, idx)
def __unicode__(self):
return self.render()
def render(self):
"""Outputs a <ul> for this set of radio fields."""
return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>'
% force_unicode(w) for w in self]))
Отредактировано (Июль 6, 2010 15:26:47)
Офлайн
Вдогонку. StrAndUnicode живет в django.utils.encoding
Не забудьте о
from django.utils.encoding import StrAndUnicode, force_unicode
from django.utils.safestring import mark_safe
Отредактировано (Июль 6, 2010 15:29:38)
Офлайн