gpt4 book ai didi

python - Django 脆皮表格 : Set rows for Text Area

转载 作者:行者123 更新时间:2023-12-04 00:16:03 24 4
gpt4 key购买 nike

使用 FormHelper() 创建表单时,我的表单的文本区域(对于 TextField s)太大:它们设置为 10 行。我想设置行数。我怎样才能做到这一点?
我的代码:models.py :

from django.db import models

class Spam(models.Model).
ham = models.CharField(max_length=10, blank=True, null=False, default='Some ham')
eggs = models.TextField(blank=True, null=False, default='', verbose_name="Lots of eggs")
forms.py :
from django import forms
from crispy_forms.helper import FormHelper
from crispyy_forms.layout import (Layout, Row, Column)

from .models import Spam

class SpamForm(forms.ModelForm):
class Meta():
model = Spam
fields = ('ham', 'eggs')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'POST'
self.helper.layout = Layout(
Row(Column('ham', css_class='form-group col-12')),
Row(Column('eggs', css_class='form-group col-12')),
# HERE: How can I set the rows for the text area widget?
)
结果 HTML:
<!-- (ommited for brevity)  -->
<div class="form-row " >
<div class="form_group col-12" rows="2">
<div id="div_id_eggs" class="form-group">
<label for="eggs" class="">Lots of eggs</label>
<div class="">
<textarea name="eggs" cols="40" rows="10" class="textarea form-control" id="eggs"></textarea>
<!-- ^^^^^^^^
<!-- THIS is what I'd like to change to "2" -->
</div>
</div>
</div>
</div>
<!-- (ommited for brevity) -->

最佳答案

您可以通过传入行来直接使用脆皮形式“覆盖”行:

Row(Column('eggs', rows='2', css_class='form-group col-12')),
或与字段:
Field('eggs', rows='2')

关于python - Django 脆皮表格 : Set rows for Text Area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63675815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com