Django脆皮表单布局

时间:2018-08-01 13:45:20

标签: django django-forms django-crispy-forms

我已经阅读了几天,无法弄清楚如何对表单进行布局。我是Django和酥脆表单的新手,这是我第一次超越简单的表单布局。

我有一个带有布尔字段(是/否)的模型。

# FOR YES / NO RADIO BOXES
BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))

class SomeModel(models.Model):
    field1 = models.BooleanField(choices=BOOL_CHOICES, blank=True)
    field1_answer = models.TextArea(blank=True)

class SomeForm(ModelForm):
    class Meta:
        model = SomeModel
        fields = ('field1', 'field1_answer')
        widgets = {
            'field1': RadioSelect
        }

    def __init__(self, *args, **kwargs):
        # CALL SELF
        super(SomeForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'

        self.helper.layout = Layout(
            Row('field1'),
            Row(
                Div(
                    Row('field1_question')
                ), css_class='hidden', id='div_field1'
            )
        )

我正在寻找的功能是对以下类似问题进行以下布局。 o是单选按钮。上面的布局远非如此。我只是提供了一些信息,让人们知道我被困在哪里。

table layout 
=================================================
| yes | no | question                           |
=================================================
|  o  |  o | FIELD 1 QUESTION                   |
=================================================
           | HIDDEN DIV IF ANSWER IS YES SHOW   |
           | FIELD1_QUESTION                    |
           ======================================

我相信使用Django表单是执行此操作的最佳方法,但是无法创建上面的布局。当用户回答是时,隐藏的div应该出现在问题的下方,要求提供更多信息。

我不需要javascript /我可以处理该部分..它是将yes / no /问题分成3列,并在问题列下的另一行中显示答案的布局。

非常感谢。经过一天的阅读,却找不到解决方案,我崩溃了!

0 个答案:

没有答案