Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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 crispy forms:2列表单,带有TextArea字段_Django_Layout_Django Forms_Textarea_Django Crispy Forms - Fatal编程技术网

Django crispy forms:2列表单,带有TextArea字段

Django crispy forms:2列表单,带有TextArea字段,django,layout,django-forms,textarea,django-crispy-forms,Django,Layout,Django Forms,Textarea,Django Crispy Forms,我正在尝试使用创建一个好看的2列表单。当其中一个字段是TextArea字段时,它会变得混乱。 (当我没有TextArea字段时,一切正常) 此代码: ## forms.py class BasicForm(forms.Form): label_1 = forms.CharField(label='label1') label_2 = forms.CharField(label='label2') label_3 = forms.CharField(label='label3',

我正在尝试使用创建一个好看的2列表单。当其中一个字段是
TextArea
字段时,它会变得混乱。 (当我没有
TextArea
字段时,一切正常)

此代码:

## forms.py
class BasicForm(forms.Form):
   label_1 = forms.CharField(label='label1')
   label_2 = forms.CharField(label='label2')
   label_3 = forms.CharField(label='label3',help_text='This is help text', widget=forms.Textarea)
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Field('label_1', wrapper_class='col-md-6', css_class='row-fluid'),
            ),

            Row(
                Field('label_2', wrapper_class='col-md-6', css_class='row-fluid'),
                Field('label_3', wrapper_class='col-md-6')
            )
        )

生成以下格式:

该代码:

       self.helper.layout = Layout(
        Row(
            Field('label_1', wrapper_class='col-md-6', css_class='row-fluid'),
            Field('label_3', wrapper_class='col-md-6')
        ),

        Row(
            Field('label_2', wrapper_class='col-md-6', css_class='row-fluid')
        )
    )
生成以下格式:

我想分开这两列,每一列都应该堆叠到顶部,只相对于它本身

编辑 (添加我想要的最终结果草图)

比如:

说我有更多的领域:

看起来您只需要稍微改变一下布局-那些
对象充当清除元素,这就是为什么
label_2
字段显示在
label_3
textarea的基线下方-因为它已被清除

因此,解决方案是使用
Div
类创建两列,然后在字段中删除
wrapper\u class
属性。您还可以删除
字段
对象,只使用字段名称(因为您不需要指定任何属性):


你能更清楚地解释一下你想要的结果是什么吗?也许你可以画一幅画并附上它?我不知道你所说的“我想把两列分开,每一列应该堆叠到顶部,只相对于它自己。”是什么意思。谢谢@谢谢你,伙计。我添加了我的最终结果应该如何相似的草图。谢谢,这太好了,很有意义。希望我的回答能让你更接近,如果你需要更多的帮助,请告诉我!这很有效,谢谢!我现在还有别的问题。如何将
label2
(例如)设置为
只读
字段,而其他字段则不是?
## forms.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Row, Div, Layout

class BasicForm(forms.Form):
   label_1 = forms.CharField(label='label1')
   # label_2 uses a widget with custom attribute(s)
   label_2 = forms.CharField(label='label2', widget=forms.TextInput(attrs={'readonly': 'readonly'}))
   label_3 = forms.CharField(label='label3',help_text='This is help text', widget=forms.Textarea)
   def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Div('label_1', 'label_2',
                    css_class='col-md-6'
                ),
                Div(
                    'label_3',
                    css_class='col-md-6'
                ),
            ),
        )