ChoiceFieldRenderer已删除。解决办法是什么? [英] ChoiceFieldRenderer removed. What is the solution?

查看:153
本文介绍了ChoiceFieldRenderer已删除。解决办法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎很少有人使用它,但是...我做到了。 在这里,您可以阅读:

It seems very few people used it, but... I did. Here you can read:


删除了django.forms.widgets中一些未记录的类:
SubWidget RendererMixin,ChoiceFieldRenderer,RadioFieldRenderer,
CheckboxFieldRenderer ChoiceInput,RadioChoiceInput ,
CheckboxChoiceInput

Some undocumented classes in django.forms.widgets are removed: SubWidget RendererMixin, ChoiceFieldRenderer, RadioFieldRenderer, CheckboxFieldRenderer ChoiceInput, RadioChoiceInput, CheckboxChoiceInput

我的源代码是:

from django.forms.widgets import ChoiceFieldRenderer, RadioChoiceInput, \
    RendererMixin, Select


class BootstrapRadioFieldRenderer(ChoiceFieldRenderer):
    outer_html = '<span {id_attr}>{content}</span>'
    inner_html = '<div class="radio">{choice_value}{sub_widgets}</div>'
    choice_input_class = RadioChoiceInput


class BootstrapRadioSelect(RendererMixin, Select):
    renderer = BootstrapRadioFieldRenderer
    _empty_value = ''

我真的不知道如何将其转换以使其在1.11及更高版本上起作用:他们说:

I really dont know how to convert this to make it work with 1.11 and later: they say:

Use a custom widget template instead.

嗯。

推荐答案

我们使用RadioFieldRenderer为每个选项添加了描述。您的用例可能还很遥远,但我希望它也可以帮助您进行迁移。

We used RadioFieldRenderer to add a description to each option. Your usecase may be far from it, but I hope it helps you to make the migration too.

这是Django< = 1.10

This was the legacy code for Django <=1.10

class MyRadioFieldRenderer(forms.widgets.RadioFieldRenderer):

    def render(self):
        radios = []
        for w in self:
            radios.append(u"""<li class="%s">%s <span>%s</span></li>"""
                          % (w.choice_value, force_unicode(w), get_description(w)))
        return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join(radios))

class MyRadioSelect(forms.RadioSelect):
    renderer = MyRadioFieldRenderer

在Django 1.11中,我使用自定义模板代码段替换了该代码,并仅在模板上下文中添加了描述。

And I replaced it with this for Django 1.11 utilizing custom template snippets and only adding the description to the template context.

from django.forms.widgets import RadioSelect

class MyRadioSelect(RadioSelect):

    template_name = 'myapp/multiple_input.html'
    option_template_name = 'myapp/input_option.html'

    def get_context(self, name, value, attrs):
        context = super(MyRadioSelect, self).get_context(name, value, attrs)

        for i in range(len(context['widget']['optgroups'][0][1])):
            value = context['widget']['optgroups'][0][1][i]['value']
            context['widget']['optgroups'][0][1][i]['attrs']['description'] = \
                get_description(value)
        return context

该深层列表中的for循环并不漂亮。

然后,我可以在模板摘要中使用< span> {{widget.attrs.description |安全}}< / span>

In the template snippet I can then render the description behind the options with <span>{{widget.attrs.description | safe}}</span>

该格式保持不变:

class MyForm(forms.Form):

    order_method = ChoiceField(
        widget=MyRadioSelect,
        required=True)






重要提示:对于Django可以找到您的自定义常规模板文件夹中的模板片段会将其添加到您的设置中:


Important: For Django to find your custom template snippets in the regular template folder add this to your settings:

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

django.forms 到您的 INSTALLED_APPS

这篇关于ChoiceFieldRenderer已删除。解决办法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆