本文介绍了Django - 在模板中单独呈现 CheckboxSelectMultiple() 小部件(手动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这两个模型:
models.py
class App(models.Model):
app_name = models.SlugField(max_length=50)
options_loaded = models.ManyToManyField(Option)
created_by = models.ForeignKey(User)
def __unicode__(self):
return self.name
class Option(models.Model):
option_name = models.SlugField(max_length=50)
condition = models.BooleanField('Enable condition')
option = models.BooleanField('Enable option1')
created_by = models.ForeignKey(User)
def __unicode__(self):
return self.option_name
我想呈现一个看起来像这样的表单,其中复选框来自不同的模型(M2M 字段的第一列,带有 CheckboxSelectMultiple() 小部件),Option_name 可以是 <a href="/link/">Option_name</a>
I would like to render a form that would look like this, where checkboxes are from different models (first column from the M2M field with CheckboxSelectMultiple() widget), and Option_name could be <a href="/link/">Option_name</a>
有可能吗?
推荐答案
这是我的简单解决方案:在模板中手动渲染 CheckboxSelectMultiple()
This is my simple solution: render CheckboxSelectMultiple() manually in template
<table>
<thead>
<tr>
<td> </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 m2moption in model.m2moptions.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() 小部件(手动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!