自定制插件widget 组件

from django.forms.widgets import RadioSelect

class ColorRadioSelect(RadioSelect):
    # input_type = 'radio'
    # template_name = 'django/forms/widgets/radio.html'
    # option_template_name = 'django/forms/widgets/radio_option.html'

    template_name = 'widgets/radio.html'
    option_template_name = 'widgets/radio_option.html'
widget.py
{% with id=widget.attrs.id %}
{#    定义id变量 id=widget.attrs.id#}
    <div{% if id %} id="{{ id }}"{% endif %}{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}>
        {% for group, options, index in widget.optgroups %}


        {% for option in options %}
            <label {% if option.attrs.id %}for=" {{ option.attrs.id }}"{% endif %}>
                {% include option.template_name with widget=option %}
            </label>

        {% endfor %}


        {% endfor %}
    </div>
{% endwith %}
radio.html
{% include "django/forms/widgets/input.html" %}
<span class="cycle" style="background-color:{{ option.label }}"></span>
radio_option.html
原文地址:https://www.cnblogs.com/hude/p/12826055.html