简体   繁体   中英

Django Crispy Forms: Set rows for Text Area

When creating a Form with FormHelper() , the text areas of my form (for TextField s) are too big: They are set to 10 rows. I'd like to set the number of rows. How can I do that?

My code:

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?
        )

Resulting 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)  -->

When creating a Form with FormHelper() , the text areas of my form (for TextField s) are too big: They are set to 10 rows. I'd like to set the number of rows. How can I do that?

My code:

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?
        )

Resulting 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)  -->

When creating a Form with FormHelper() , the text areas of my form (for TextField s) are too big: They are set to 10 rows. I'd like to set the number of rows. How can I do that?

My code:

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?
        )

Resulting 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)  -->

There are many ways to achieve this, but you can try with django widgets.

text = forms.CharField(
        widget=forms.Textarea(attrs={"rows": 7, "cols": 40}),
        label="Custom field label.",
    )

And then call your field by name in crispy forms Layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM