在django表单中覆盖RadioSelect的渲染会产生意外结果

时间:2018-02-01 10:44:03

标签: django forms radio-button widget

我想在django中覆盖RadioSelect的渲染。 (我为复选框做了类似的事情,我希望两者看起来都一样)。这个的一般工作流程是编写自定义渲染器,然后在ChoiceInput中更改渲染,但是当我复制现有代码并运行它时,html输出不安全并且显示了转义的html字符串。这没有任何意义,因为除了更改名称之外,我还没有对课程进行任何更改:

在我的widgets.py中:

class ButtonRadioFieldRenderer(ChoiceFieldRenderer):
    choice_input_class = OtherRadioChoiceInput


class OtherRadioChoiceInput(OtherChoiceInput):
    input_type = 'radio'

    def __init__(self, *args, **kwargs):
        super(OtherRadioChoiceInput, self).__init__(*args, **kwargs)
        self.value = force_text(self.value)


@html_safe
@python_2_unicode_compatible
class OtherChoiceInput(SubWidget):
    """
    An object used by ChoiceFieldRenderer that represents a single
    <input type='$input_type'>.
    """
    input_type = None  # Subclasses must define this

    def __init__(self, name, value, attrs, choice, index):
        self.name = name
        self.value = value
        self.attrs = attrs
        self.choice_value = force_text(choice[0])
        self.choice_label = force_text(choice[1])
        self.index = index
        if 'id' in self.attrs:
            self.attrs['id'] += "_%d" % self.index

    def __str__(self):
        return self.render()

    def render(self, name=None, value=None, attrs=None, choices=()):
        if self.id_for_label:
            label_for = format_html(' for="{}"', self.id_for_label)
        else:
            label_for = ''
        attrs = dict(self.attrs, **attrs) if attrs else self.attrs
        return format_html(
            '<label{}>{} {}</label>', label_for, self.tag(attrs), self.choice_label
        )

    def is_checked(self):
        return self.value == self.choice_value

    def tag(self, attrs=None):
        attrs = attrs or self.attrs
        final_attrs = dict(attrs, type=self.input_type, name=self.name, value=self.choice_value)
        if self.is_checked():
            final_attrs['checked'] = 'checked'
        return format_html('<input{} />', flatatt(final_attrs))

    @property
    def id_for_label(self):
        return self.attrs.get('id', '')

在我的forms.py中:

DELIMITER_CHOICES = [
    ('space', ugettext_lazy("space")),
    ('underscore', "_"),
    ]

class SingleDelimiterForm(forms.Form):

    delimiter = forms.ChoiceField(initial=0, widget=forms.RadioSelect(renderer=ButtonRadioFieldRenderer), choices=DELIMITER_CHOICES)

我做的唯一改变就是把#34;其他&#34;和&#34;按钮&#34;在已有的类前面,代码不再运行。如果我将OtherChoiceInput更改为ChoiceInput,则代码正在运行。 (最后我只想在标签上添加一个类......)

1 个答案:

答案 0 :(得分:0)

我需要unicode字符串才能使代码正常工作,因此from __future__ import unicode_literals解决了问题。我仍然觉得这个错误非常混乱。