POST数据从表单到表单

时间:2014-03-11 22:47:12

标签: django forms

我想使用POST数据来部分预填充下一个表单。

我在一个页面上有一个表格......

<form method="POST" action='{% url 'screen_many' %}'>

将内容传递给显示第二种形式的视图。

def screen_many(request):
    #...
    if request.method == 'POST':
        form = SurveyForm(request.POST)
        if form.is_valid():
            saved = form.save(commit=False)
            saved.save()
            return HttpResponseRedirect('/')
    else:
        form = SurveyForm(initial={'student': request.POST['selected_student']})

当然,if分支被采用,第二种形式抱怨验证错误。

编辑:根据要求,我的表格代码......

class SurveyForm(forms.ModelForm):

    nsf_CHOICES = (
        (1, 'Never',), 
        (2, '',),
        (3, 'Sometimes',),
        (4, '',),
        (5, 'Frequently',),
    ) 

    nmv_CHOICES = (
        (1, 'Not Descriptive'),
        (2, ''),
        (3, 'Moderately Descriptive'),
        (4, ''),
        (5, 'Very Descriptive'),
    )

    aggbs_0 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_0')[0].verbose_name, empty_value=0)
    aggbs_1 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_1')[0].verbose_name, empty_value=0)
    aggbs_2 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_2')[0].verbose_name, empty_value=0)
    aggbs_3 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_3')[0].verbose_name, empty_value=0)
    aggbs_4 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_4')[0].verbose_name, empty_value=0)
    aggbs_5 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_5')[0].verbose_name, empty_value=0)
    aggbs_6 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_6')[0].verbose_name, empty_value=0)
    aggbs_7 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_7')[0].verbose_name, empty_value=0)

    si_0 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_0')[0].verbose_name, empty_value=0)
    si_1 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_1')[0].verbose_name, empty_value=0)
    si_2 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_2')[0].verbose_name, empty_value=0)
    si_3 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_3')[0].verbose_name, empty_value=0)
    si_4 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_4')[0].verbose_name, empty_value=0)
    si_5 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_5')[0].verbose_name, empty_value=0)
    si_6 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_6')[0].verbose_name, empty_value=0)
    si_7 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nmv_CHOICES, label=Survey._meta.get_field_by_name('si_7')[0].verbose_name, empty_value=0)

    cfiab_0 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_0')[0].verbose_name, empty_value=0)
    cfiab_1 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_1')[0].verbose_name, empty_value=0)
    cfiab_2 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_2')[0].verbose_name, empty_value=0)
    cfiab_3 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_3')[0].verbose_name, empty_value=0)
    cfiab_4 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_4')[0].verbose_name, empty_value=0)
    cfiab_5 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_5')[0].verbose_name, empty_value=0)
    cfiab_6 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_6')[0].verbose_name, empty_value=0)
    cfiab_7 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfiab_7')[0].verbose_name, empty_value=0)

    cfimb_0 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_0')[0].verbose_name, empty_value=0)
    cfimb_1 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_1')[0].verbose_name, empty_value=0)
    cfimb_2 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_2')[0].verbose_name, empty_value=0)
    cfimb_3 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_3')[0].verbose_name, empty_value=0)
    cfimb_4 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_4')[0].verbose_name, empty_value=0)
    cfimb_5 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_5')[0].verbose_name, empty_value=0)
    cfimb_6 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_6')[0].verbose_name, empty_value=0)
    cfimb_7 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_7')[0].verbose_name, empty_value=0)
    cfimb_8 = forms.TypedChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('cfimb_8')[0].verbose_name, empty_value=0)

    class Meta:
        model = Survey

    def __init__(self, *args, **kwargs):

        self.helper = FormHelper()
        self.form_method = 'post'
        self.helper.form_tag = False
        self.helper.layout = Layout(
                'surveyset',
                'student',
                Fieldset('Critical Events Index',
                        HTML("""
                        <p>Check each behavior from the list below that the child has exhibited during this school year. Complete this using your knowledge of during this school year.</p>
                        """),
                        'cei_0','cei_1','cei_2','cei_3','cei_4','cei_5','cei_6','cei_7','cei_8','cei_9','cei_10','cei_11','cei_12','cei_13','cei_14','cei_15',),

                Fieldset('Aggressive Behavior Scale',
                        HTML("""
                        <p>The numbers one through five are used to show the estimate of the frequency with which each item occurs. Choose the number that best represents the amount of times this behavior happens. Complete this scale using your knowledge of during this school year.</p>
                        """),'aggbs_0','aggbs_1','aggbs_2','aggbs_3','aggbs_4','aggbs_5','aggbs_6','aggbs_7',),

                Fieldset('Social Interaction',
                        HTML("""
                        <p>The numbers one through seven are used to show the estimate of the frequency with which each item occurs. Choose the number that best represents the amount of times this behavior happens. Complete this scale using your knowledge of during this school year.</p>
                        """),'si_0','si_1','si_2','si_3','si_4','si_5','si_6','si_7',),

                Fieldset('Combined Frequency Index Adaptive Behavior',
                    HTML("""
                        <p>The numbers one through five are used to show the estimate of the frequency with which each item occurs. Choose the number that best represents the amount of times this behavior happens. Complete this scale using your knowledge of during this school year.</p>
                        """), 'cfiab_0','cfiab_1','cfiab_2','cfiab_3','cfiab_4','cfiab_5','cfiab_6','cfiab_7',),

                Fieldset('Combined Frequency Index Maladaptive Behavior',
                    HTML("""<p>The numbers one through five are used to show the estimate of the frequency with which each item occurs. Choose the number that best represents the amount of times this behavior happens. Complete this scale using your knowledge of during this school year.</p>"""),
                'cfimb_0','cfimb_1','cfimb_2','cfimb_3','cfimb_4','cfimb_5','cfimb_6','cfimb_7','cfimb_8',),

                )
        super(SurveyForm, self).__init__(*args, **kwargs)

我的模特......

0 个答案:

没有答案