脆皮形式的条件逻辑

Conditional logic for crispy form

我有一个表单,我想知道在用户为 typeOfTheproject 字段选择“Web 应用程序”之前,我应该如何隐藏字段“conditionalWeb”?

我已经在网上进行了研究,但我完全不知道如何继续...任何帮助都会很好 :)

from django import forms
from configurator import models
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
from .models import TypeOfProgram, Language, Framework, Database
from crispy_forms.bootstrap import (PrependedAppendedText, PrependedText, FormActions)
    
class ConfiguratorForm(forms.Form):
    helper = FormHelper()
    helper.form_method = 'POST'
    helper.form_show_labels = False

    queryOfProject = TypeOfProgram.objects.values_list('name')
    queryOfFramework = Framework.objects.values_list('name','version')
    queryOfDatabase = Database.objects.values_list('name','version')
    listFramework = []
    listProject = []
    conditionalWeb=[]
    listFramework=[((q[0],q[1]),q[0]+" version "+q[1])for q in queryOfFramework]
    listProject=[(q[0],q[0])for q in queryOfProject]
    listDatabase = [((q[0],q[1]),q[0]+" version "+q[1])for q in queryOfDatabase]

    typeOfTheproject = forms.ChoiceField(choices = listProject)
    conditionalWeb = forms.ChoiceField (choices = [('nothing', '----'),("Only Backend","Only Backend"),("Only Frontend","Only Frontend")])
    wantedFramework = forms.MultipleChoiceField(choices = listFramework)
    wantedDatabase = forms.MultipleChoiceField(choices = listDatabase)
    helper.layout = Layout(
        'typeOfTheproject',
        'wantedFramework',
        'wantedDatabase',
        FormActions(Submit('Finalize and find the result','Finalize and find the result', css_class="btn btn-success")) 
    )
#Not Working
    if typeOfTheproject is 'Web application':
        helper.layout.append('conditionalWeb')

谢谢:)

您可以为任务添加一个简单的javascript:

$(document).ready(function(){
    hideShow()
})

// call hideShow when the user clicks on the project_type dropdownlist
$('#id_typeoftheproject').click(function(){
    hideShow()
});
function hideShow(){
    if(document.getElementById('id_typeoftheproject').value == "7")
    {
        $('#id_conditionalweb').show();
    }
else 
    {
    $('#id_conditionalweb').hide();
    }
}

您需要从数据库中找到字段的实际 ID 并替换 #id_typeoftheproject#id_conditionalweb。此外,值“7”需要替换为 Web 应用程序的 ID。