django 1.6 form.as_p如何决定textfield行和cols?

时间:2014-01-31 13:26:17

标签: django

我已经阅读了form.as_p方法的源代码,但仍然不知道。enter image description here

#models.py

class User(AbstractBaseUser):

    description = models.TextField(max_length=200,blank=True,verbose_name=_('个人说明'))
    #rest fields omitted...

#forms.py
class ProfileForm(forms.ModelForm): 
    class Meta:
        model = User
        fields = [ 'description', ]

#template.html,edit the profile
<form enctype="multipart/form-data" action="" method="post">{% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="提交" />
</form>

这是源代码:

def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
    "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
    output, hidden_fields = [], []

    for name, field in self.fields.items():
        html_class_attr = ''
        bf = self[name]
        # Escape and cache in local variable.
        bf_errors = self.error_class([conditional_escape(error) for error in bf.errors])
        if bf.is_hidden:
            if bf_errors:
                top_errors.extend(
                    [_('(Hidden field %(name)s) %(error)s') % {'name': name, 'error': force_text(e)}
                     for e in bf_errors])
            hidden_fields.append(six.text_type(bf))
        else:
            # Create a 'class="..."' atribute if the row should have any
            # CSS classes applied.
            css_classes = bf.css_classes()
            if css_classes:
                html_class_attr = ' class="%s"' % css_classes

            if errors_on_separate_row and bf_errors:
                output.append(error_row % force_text(bf_errors))

            if bf.label:
                label = conditional_escape(force_text(bf.label))
                label = bf.label_tag(label) or ''
            else:
                label = ''

            if field.help_text:
                help_text = help_text_html % force_text(field.help_text)
            else:
                help_text = ''

            output.append(normal_row % {
                'errors': force_text(bf_errors),
                'label': force_text(label),
                'field': six.text_type(bf),
                'help_text': help_text,
                'html_class_attr': html_class_attr
            })
    if top_errors:
        output.insert(0, error_row % force_text(top_errors))

    if hidden_fields: # Insert any hidden fields in the last row.
        str_hidden = ''.join(hidden_fields)
        if output:
            last_row = output[-1]
            # Chop off the trailing row_ender (e.g. '</td></tr>') and
            # insert the hidden fields.
            if not last_row.endswith(row_ender):
                # This can happen in the as_p() case (and possibly others
                # that users write): if there are only top errors, we may
                # not be able to conscript the last row for our purposes,
                # so insert a new, empty row.
                last_row = (normal_row % {'errors': '', 'label': '',
                                          'field': '', 'help_text':'',
                                          'html_class_attr': html_class_attr})
                output.append(last_row)
            output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
        else:
            # If there aren't any rows in the output, just append the
            # hidden fields.
            output.append(str_hidden)
    return mark_safe('\n'.join(output))

1 个答案:

答案 0 :(得分:2)

您可以在窗口小部件的attrs中指定用于表示模型字段的行和列。

在这种情况下,模型的TextFieldTextarea小部件表示。所以下面的代码应该可以工作。

#forms.py
from django import forms

class ProfileForm(forms.ModelForm)
    class Meta:
        model = User
        fields = [ 'description', ]
        widgets = {
          'description': forms.Textarea(attrs={'rows':4, 'cols':15}),
        }

如果你想看到这背后的来源,你可能想要深入研究Textarea小部件的代码。以下是确切行的链接供您参考:

https://github.com/django/django/blob/master/django/forms/widgets.py#L405