【问题标题】:Add CSS class to django RadioSelect label将 CSS 类添加到 django RadioSelect 标签
【发布时间】:2014-04-26 05:38:54
【问题描述】:

所以我有一些这样的小部件设置

widgets = {
        'history': Select(attrs={'ng-model': 'data.smile_assessment.history'}),
        'concern': Select(attrs={'ng-model': 'data.smile_assessment.concern'}),
        'crowding': RadioSelect(attrs={'ng-model': 'data.smile_assessment.crowding'}),
        'spacing': RadioSelect(attrs={'ng-model': 'data.smile_assessment.spacing'}),
        'age_group': Select(attrs={'ng-model': 'data.smile_assessment.age_group'}),
        'gender': Select(attrs={'ng-model': 'data.smile_assessment.gender'}),
    }

然后渲染成这样

<li>{{ form.history }}</li>
<li>{{ form.concern }}</li>
<li>{{ form.crowding }}</li>
<li>{{ form.spacing }}</li>
<li>What is your age group: {{ form.age_group }}</li>
<li>What is your gender: {{ form.gender }}</li>

我想要做的是将相同的 css 类应用于两个 RadioSelect 表单的标签。 目前标签是这样的

<label for="id_2"></label>

我希望它看起来像这样

<label class="myclass" for="id_2"></label>

不太确定该怎么做,我可以修改 RadioSelect 小部件的属性,但这只能让我设置输入标签的类,而不是标签标签。

【问题讨论】:

    标签: django python-2.7 django-forms


    【解决方案1】:

    对于那些将要查看这个问题的人,我发现了一个技巧......我不知道 RadioSelect() 小部件是怎么回事......我这样渲染了我的模板:

    {% for choice in form.category_res %}
            <div class="custom-control custom-radio custom-control-inline">
                {{ choice.tag }}
                <label for="{{ choice.id_for_label  }}" class="custom-control-label">{{ choice.choice_label }}</label>
            </div>
    {% endfor %}
    

    (category_res) 是我有选择的领域 我知道将 django2.0 与 python3.6 一起使用,并且我在模板上使用了 bootstrap4.0 ......我希望这对你有用

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2020-04-13
      • 2020-12-21
      • 2018-07-31
      • 2011-03-10
      • 2011-01-24
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      相关资源
      最近更新 更多