我想在表单中的Queryset中显示一个以上的值,但无法获取这些值。 我试图使用forms.locations.all并遍历它们。 forms.locations.1看起来可以使用,但是forms.locations.1.name forms.locations.1.street也不起作 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我一直在网上搜索如何将颜色/主题应用于django中的复选框表单项,但找不到如何操作。
我想做的事情可能很简单,但是尽管所有主题和博客都是红色的,我仍然无法做到。
所以这是我的表格简化:
class MyForm(forms.Form):
def __init__(self, data, *args, **kwargs):
super(MyForm, self).__init__(data, *args, **kwargs)
self.fields['checkbox'] = forms.MultipleChoiceField(
label='Set of questions to know you better',
required=True,
widget=forms.CheckboxSelectMultiple(attrs={'disabled': 'disabled'}),
choices=((1, 'Proposition 1'), (2, 'Proposition 2'), (3, 'Proposition 3'), (4, 'Proposition 4')),
initial=(1, 2, 4,)
)
而且我想知道如何修改我目前看起来的html
<form action="{% url 'dummy' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
</form>
所以初始设置为True的所有选项都有一个给定的css应用于它们,而那些设置为False的另一个选择。
希望我的问题有道理。 如果没有,请不要犹豫。
最好:
埃里克
编辑:添加生成的HTML
<form action="/dummy" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='LGZ0mwqrAKxb7CzFvOyYIyUIcDSARy4iwEtHVDjKPpnBd1AIXlk4UyxRuCSIEviY' />
<p><label>Set of questions to know you better :</label> <ul id="id_checkbox">
<li><label for="id_checkbox_0"><input type="checkbox" name="checkbox" value="1" disabled="disabled" id="id_checkbox_0" checked />
Proposition 1</label>
</li>
<li><label for="id_checkbox_1"><input type="checkbox" name="checkbox" value="2" disabled="disabled" id="id_checkbox_1" checked />
Proposition 2</label>
</li>
<li><label for="id_checkbox_2"><input type="checkbox" name="checkbox" value="3" disabled="disabled" id="id_checkbox_2" />
Proposition 3</label>
</li>
<li><label for="id_checkbox_3"><input type="checkbox" name="checkbox" value="4" disabled="disabled" id="id_checkbox_3" checked />
Proposition 4</label>
</li>
</ul></p>
</form>
如果你需要设置检查输入的样式,你可以使用CSS伪类:checked
,这样:
input:checked {
/*some code here */
}
但是,如果您需要有条件地将类添加到输入中,则可以在实例化表单的视图中执行此操作并覆盖create_option
方法,但我认为创建自定义模板标记并使用它更容易。 您可以尝试以下内容:
from django import template
@register.filter
def add_css(checkbox_input):
checkbox_input.data['attrs'].update({"class":"your-class"})
return checkbox_input
然后在模板中
{% for x in form.checkbox %}
{% if x.data.selected %}
{{ x|add_css }}
{% else %}
{{ x }}
{% endif %}
{% endfor %}
尝试基于CheckboxSelectMultiple创建一个新的Widget,覆盖create_option
方法。
如果选择了选项,则添加新的类属性。
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
[..]
if selected:
option_attrs.update(self.checked_attribute)
# add your class here to option_attrs
[..]
好的,您也可以使用jQuery或vanilla js动态添加类,因为我总是使用jQuery(你也应该使用它,它的代码比vanilla js少得多)我将提供jQuery解决方案:
<script "src={% static 'jquery.js' %}" type="text/javascript"></script> // provide path to jquery or use cdn, whatever just add jquery
<script>
$(document).ready(function() {
// On document ready add relevant classes to each checkbox
$('input[name=checkbox]').each(function(){
if($(this).is(':checked')){
$(this).addClass('checked');
}
else{
$(this).addClass('unchecked');
}
console.log(this);
});
// On checkbox change, remove classes and add new class based on if its checked or not
$('input[name=checkbox]').change(function(){
$(this).removeClass('checked unchecked');
if($(this).is(':checked')){
$(this).addClass('checked');
}
else{
$(this).addClass('unchecked');
}
console.log(this);
});
});
</script>
jsFiddle演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.