Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 根据同一格式中另一个变量的值设置一个变量的选择字段_Django_Django Forms - Fatal编程技术网

Django 根据同一格式中另一个变量的值设置一个变量的选择字段

Django 根据同一格式中另一个变量的值设置一个变量的选择字段,django,django-forms,Django,Django Forms,我正试图根据django模板中表单变量的值绘制图形。 我的表单中有4个变量(可选字段) 我希望我的第二个选项字段更改与第一个变量的值相关的选项 例如,是var1='age'var2_options=['M','F'] 如果是var1='Education',var2_options=['学士','硕士','高中'] 这是我的密码: views.py- class HomeView(TemplateView): template_name='charts.html' def get(self,

我正试图根据django模板中表单变量的值绘制图形。 我的表单中有4个变量(可选字段) 我希望我的第二个选项字段更改与第一个变量的值相关的选项 例如,是var1='age'var2_options=['M','F'] 如果是var1='Education',var2_options=['学士','硕士','高中']

这是我的密码:

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(模板)

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

请按照vitors的说明进行操作


请按照vitors的说明进行操作

<form method="POST">
            {%csrf_token%}
            {{form.as_ul}}
            <button type="submit">get</button>
            {{form.as_ul}}
            <button type="submit">PLOT GRAPH</button>
        </form>
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')))