Как передать аргумент значению в формах. BooleanField - PullRequest
0 голосов
/ 15 мая 2019

Я пытаюсь передать аргумент значению в поле формы, но в шаблоне он отображает этот аргумент как строку символов, а не как переменную.

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

1 Ответ

0 голосов
/ 15 мая 2019
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
}

при условии, что us.code_ue имеет значение True / False (т. Е. Логическое значение).

Если вы пытаетесь присвоить ему метку, тогда form.fields['checkbox'].label = ue.code_ue должен прийти после того, как вы инициировали форму.render (запрос, 'MyHTML.html', контекст)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...