【问题标题】:Setting the choice field of one variable according to the value of another variable in the same form根据同形式的另一个变量的值设置一个变量的选择域
【发布时间】:2019-09-13 06:45:59
【问题描述】:

我正在尝试根据我的 django 模板中的表单变量的值绘制图表。 我的表单中有 4 个变量(ChoiceFields) 我希望我的第二个选择字段更改关于第一个变量的值的选择 例如。是 var1='年龄' var2_options=['M','F'] 如果是 var1='Education', var2_options=['Bachelors','Masters','High School']

这是我的代码:

views.py-

class HomeView(TemplateView):
template_name='charts.html'

def get(self, request):
    form = HomeForm()
    return render(request, self.template_name, {'form':form})
def post(self,request):
    form=HomeForm(request.POST)
    if form.is_valid():
        text_1 = form.cleaned_data['post_1']
        global z
        z=text_1
        text = form.cleaned_data['post']
        global b
        b=text
        text1 = form.cleaned_data['post1']
        global c
        c=text1
        text2 = form.cleaned_data['post2']
        global d
        d=text2
    args = {'form':form, 'text_1':text_1,'text':text, 'text1':text1, 'text2':text2}
    return render(request, self.template_name, args)

charts.html(模板)

<form method="POST">
            {%csrf_token%}
            {{form.as_ul}}
            <button type="submit">get</button>
            {{form.as_ul}}
            <button type="submit">PLOT GRAPH</button>
        </form>

forms.py

class HomeForm(forms.Form):

post_1 = forms.ChoiceField(choices=((None,None),('लिंग :','sex :'),('शिक्षण:','education:'),('जात :','caste :'),('व्यवसाय :','occupation :'))) 
post = forms.ChoiceField(choices=((None,None),('लिंग :','लिंग :'),('शिक्षण:','शिक्षण:'),('जात :','जात :'),('व्यवसाय :','व्यवसाय :'))) 
post1 = forms.ChoiceField(choices=((None,None),('लिंग :','लिंग :'),('शिक्षण:','शिक्षण:'),('जात :','जात :'),('व्यवसाय :','व्यवसाय :'))) 
post2 = forms.ChoiceField(choices=((None,None),('bar','bar'),('horizontalBar','horizontalBar'),('line','line'))) 

我为每个变量准备了一系列选择 如何传递一个变量,然后根据它分配下一个变量的选择字段?

【问题讨论】:

    标签: django django-forms


    【解决方案1】:

    在此处按照 vitors 的说明进行操作

    How to Implement Dependent/Chained Dropdown List with Django

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2014-10-06
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多