简体   繁体   English

Django 表单小部件中文本的垂直对齐方式

[英]Vertical alignment of text in Django form widget

I have a multi-line input field in my form.我的表单中有一个多行输入字段。 The text is aligned in the middle, but I want it aligned to the top.文本在中间对齐,但我希望它与顶部对齐。

My form:我的表格:

class FeedbackForm(forms.ModelForm):
    submitter = forms.CharField(max_length=100, help_text="Name")
    sub_email = forms.EmailField(help_text="E-Mail Adresse (wird nicht veröffentlicht.)")
    sub_date = forms.DateField(disabled=True, help_text="Datum", initial=date.today())
    content = forms.CharField(max_length=500, help_text="Text",
        widget=forms.TextInput(attrs={'vertical-align': 'top'}))

My template:我的模板:

<form id="category_form" method="POST"> 
    {% csrf_token %}
    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    <table>
       <tr>
           <td><strong>Datum</strong></td>
           <td>{{ form.sub_date }}{{ form.sub_date.errors }}</td>
        </tr>
       <tr>
           <td><strong>Name</strong></td>
           <td>{{ form.submitter }}{{ form.submitter.errors }}</td>
        </tr>
       <tr>
           <td><strong>E-Mail</strong></td>
           <td>{{ form.sub_email }}{{ form.sub_email.errors }}</td>
        </tr>
       <tr>
           <td><strong>Text</strong></td>
           <td>{{ form.content }}{{ form.content.errors }}</td>
        </tr>
    </table>

    <p><input type="submit" name="submit" value="Save" /></p>
</form>

I'm trying to pass the CSS style in the attrs attribute of the widget, as described in the Django docs.我正在尝试在小部件的 attrs 属性中传递 CSS 样式,如 Django 文档中所述。 But my text is still aligned in the middle.但我的文字仍然在中间对齐。 What am I doing wrong?我究竟做错了什么?

Figured it out.弄清楚了。

What you do is use the Textarea widget instad of TextInput:你要做的是使用 TextInput 的 Textarea 小部件:

content = forms.CharField(max_length=500, help_text="Text",
        widget=forms.Textarea()

Then you can apply the style in the CSS:然后你可以在 CSS 中应用样式:

textarea, input, button, select { 
    font-family: inherit; font-size: inherit; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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