在Django中为单选按钮设置ID [英] Setting ID for Radio buttons in Django

查看:171
本文介绍了在Django中为单选按钮设置ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.4允许 radio select

It is great that Django 1.4 allows fine graining of radio select

{% for radio in form.important_client reversed%}
      {{radio.tag}}<label for="????">{{radio.choice_label}}</label>
{% endfor %}

但由于某种奇怪的原因,使用此方法时,< input> 没有ID。因此,我无法相应地设置< label for ='ID'/>

but for some odd reason when using this methodology, the <input> have no IDs. And hence I can't set the <label for='ID' /> accordingly. That causes big issues in my CSS.

是否仍然有设置ID的设置?

Is there anyway to get the IDs set nonetheless?

推荐答案

在调试RadioSelect渲染时,我不知道如何优雅地使用radio标签和标签。因此,这是我为您解决问题的尝试:

While debuging a RadioSelect rendering, I got no idea of using radio tag and label elegantly. So here is my attempt to solve your problem:

{% for radio in form.important_client reversed %}
    <input name="{{ radio.name }}" type="radio" id="radio_{{ radio.index }}" value={{ radio.choice_value }}>
    <label for="radio_{{ radio.index }}">{{ radio.choice_label }}</label>
{% endfor %}

而不是 radio.index 属性(未记录),可以使用 forloop.counter

Instead of radio.index property, which is not documented, you can use forloop.counter.

只需输入案例中,我附上了调试窗口的屏幕截图,其中显示了 radio 上下文的示例(图中的 form_of_field 变量):

Just in case I attach a screenshot of debug window, where example of radio context is shown (form_of_field variable on a figure):

这篇关于在Django中为单选按钮设置ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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