Django:CheckboxSelectMultiple [英] Django: CheckboxSelectMultiple

查看:113
本文介绍了Django:CheckboxSelectMultiple的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以单独呈现每个复选框,而不是必须将列表中的所有复选框聚集在一起,默认情况下是这样的?像

Is it possible to render each checkbox individually, instead of it having to clump all the checkboxes together in a list, as it does by default? Something like

{{ myform.cbmultiple.0 }}

仅渲染第一个复选框?实际上, 0 将必须是一个变量,所以我可以循环...

To render just the first checkbox? Actually the 0 would have to be a variable so I can loop...

我问的原因是因为我想以相当复杂的方式显示这些复选框,所以默认的小部件不适用于我。我也不希望覆盖该小部件,因为使用模板语法比使用python代码更容易地渲染它,加上这是一次性的很多工作。

The reason I'm asking is because I want to display these checkboxes in a rather complicated way, so the default widget doesn't work for me. I also don't really want to override the widget because it's much easier to render it using the template syntax than in python code, plus, that's a lot of work for a one-off.

推荐答案

有一种方法可以在模板中手动呈现CheckboxSelectMultiple(),以便您可以使用它来完成所需的任务。

There is a way to render the CheckboxSelectMultiple() manually in the template so you can do what you want with it.

详细了解此帖

解决方案可能是这样的:

The solution could be something like this:

<table>
<thead>
  <tr>
  <td>&nbsp;</td>
  <td>V</td>
  <td>S</td>
  </tr>
</thead>    
{% for pk, choice in form.options.field.widget.choices %}
<tr>
<td><a href="/link/{{ choice }}">{{ choice }}</a></td>
<td><label for="id_options_{{ forloop.counter0 }}"><input {% for option in app.options.all %}{% if option.pk == pk %}checked="checked"{% endif %}{% endfor %} type="checkbox" id="id_options_{{ forloop.counter0 }}" value="{{ pk }}" name="options" /></label></td>
</tr>
{% endfor %}                
</table>

这篇关于Django:CheckboxSelectMultiple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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