如何在Django表单中创建“小孩”问题? [英] How to create 'child' questions in Django forms?

查看:81
本文介绍了如何在Django表单中创建“小孩”问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Django表单创建小孩问题。这是根据上述问题提供的答案只能看到的问题。

I would like to know how to create 'child' questions using Django forms. Thats is, questions which are only visible depending on the answer provided in a preceding question.

我很确定我没有使用正确的术语孩子的问题,但我相信有人会弄清楚我的意思

I'm fairly sure I'm not using the correct terminology with the term 'child' questions but I am sure someone will figure out what I mean

在下面的示例中,当用户选择美国时,选择他们来自哪个状态的选项应该可见。这将是我网站表单上的标准。

In the below example, when the user selects "United States of America" The option to select which state they are from should become visible. This would be standard on my website forms.

有没有标准的功能可以做到这一点?

Is there a standard functionality available to do this?

国家列表

   COUNTRY = (   
        ("", "----------"), 
        (AFGHANISTAN, 'Afghanistan'),
        (ALBANIA, 'Albania'),
        ....
        (UNITED STATES OF AMERICA, 'United States of America'),
        ....
        (ZAMBIA, 'Zambia'),
        (ZIMBABWE, 'Zimbabwe'),

        COUNTRY = forms.ChoiceField(widget=forms.Select(), choices=COUNTRY, initial="", label='What Country are you from?')

状态列表

STATE = (
        ("", "----------"), 
        (ALABAMA, 'Alabama'),
        (ALASKA, 'Alaska'),
        (ARIZONA, 'Arizona'),
        ....
        ....
        (WYOMING, 'Wyoming'),
        (DISTRICT_OF_COLUMBIA, 'District of Columbia'),
        (PUERTO_RICO, 'Puerto Rico'),
        (GUAM, 'Guam'),
        (AMERICAN_SAMOA, 'American Samoa'),
        (US_VIRGIN_ISLANDS, 'US Virgin Islands'),
        (NORTHERN_MARIANO_ISLANDS, 'Northern Mariana Islands'),
            )

    state = forms.ChoiceField(widget=forms.Select(), choices=STATE, initial="", label='What State are you from?')


推荐答案

您应该可以使用javascript来显示/隐藏子选择器,您处理您的表单,只有在国家/地区值为美国时才保存状态值。

You should probably just use javascript to show/hide the "child" selector, and then when you process your form, only save a state value if the country value is USA.

这篇关于如何在Django表单中创建“小孩”问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆