我尝试将一个参数传递给表单字段中的value,但是在模板中,该参数将其显示为字符串而不是变量。
checkbox =forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class':'checkbox', 'value':'{{ ue.code_ue }}'}))
def liste_ue(request, filiere):
if request.method == 'POST':
supp_ue_bloc_form = SuppUEBlocForms(request.POST);
if supp_ue_bloc_form.is_valid():
liste = supp_ue_bloc_form.cleaned_data['action'];
check = request.POST.getlist('checkbox');
return HttpResponse(check);
else:
return HttpResponse(supp_ue_bloc.is_valid());
else:
ues = UE.objects.filter(filiere__nom_filiere=filiere);
supp_ue_bloc_form = SuppUEBlocForms(initial={'checkbox':ues[0].code_ue})
return render(request, 'felyn/admin/liste_ue.html', {'supp_ue_bloc_form': supp_ue_bloc_form,\
'ues': ues, 'filiere': request.session.get('filiere')});
<table class="table table-striped">
<caption class="">Liste des UEs de la filière {{ filiere }}</caption>
<thead>
<tr>
<th><input type="checkbox" id="checkall"/></th>
<th>Code</th>
<th>Intitulé</th>
<th>Type</th>
<th>Niveau</th>
<th>Filière</th>
<th>Semestre</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
{% for ue in ues %} <!-- <input type="checkbox" name="checkbox" class="checkbox" value={{ ue.code_ue }} />-->
<tr>
<th> {{ supp_ue_bloc_form.checkbox.errors }} {{ supp_ue_bloc_form.checkbox }}</th>
<td>{{ ue.code_ue }}</td>
<td>{{ ue.intitule_ue }}</td>
<td>{{ ue.type_ue }}</td>
<td>{{ ue.niveau }}</td>
<td>{{ ue.filiere }}</td>
<td>{{ ue.semestre }}</td>
<td><a href="{% url 'supprimer_ue' code=ue.code_ue %}">Supprimer</a></td>
<td><a href="{% url 'modifier_ue' code=ue.code_ue %}">Modifier</a></td>
</tr>
{% endfor %}
</tbody>
如您所见,value的值在每一行上都不相同,我需要它的值来执行ttraitemess
答案 0 :(得分:0)
class MyForm (forms.Form):
checkbox =forms.BooleanField(required=False, widget=forms.CheckboxInput(attrs={'class':'checkbox'}))
您认为
form = MyForm(initial={'checkbox':ue.code_ue})
context = {
'form':form
}
假设我们。code_ue为True / False(即布尔值)。
如果您想给它加上标签,那么
form.fields['checkbox'].label = ue.code_ue
应该在您启动表单之后出现。
渲染(请求,“ MyHTML.html”,上下文)