Python 在 django admin 中控制 TextArea 小部件的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18738486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Control the size TextArea widget look in django admin
提问by Oz123
I managed to override the look of a TextArea
Widget in the django admin interface with two different ways:
我设法TextArea
用两种不同的方式在 django 管理界面中覆盖了小部件的外观:
using formfield_overrides
使用 formfield_overrides
in admin.py
:
在admin.py
:
class RulesAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': Textarea(
attrs={'rows': 1,
'cols': 40})},
}
...
admin.site.register(Rules, RulesAdmin)
This way is a bit of an overkill, since it will change all the TextField for that model.
这种方式有点矫枉过正,因为它会更改该模型的所有 TextField。
with a custom form:
使用自定义表单:
in forms.py
:
在forms.py
:
from django.forms import ModelForm, Textarea
from TimePortal.models import Rules
class RulesModelForm(ModelForm):
class Meta:
model = Rules
widgets = {
'parameters': Textarea(attrs={'cols': 30, 'rows': 1}),
}
in admin.py
在 admin.py
from AppName.forms import RulesModelForm
class RulesAdmin(admin.ModelAdmin):
form = RulesModelForm
Both solutions resize the TextArea
. However in both solutions the actual size of the
text area is more than 1 row (actually 2 rows). Here is how the rendered HTML looks like:
两种解决方案都会调整TextArea
. 然而,在这两种解决方案中,文本区域的实际大小都超过 1 行(实际上是 2 行)。下面是呈现的 HTML 的样子:
<div class="form-row field-parameters">
<div>
<label for="id_parameters" class="required">Parameters:</label>
<textarea id="id_parameters" rows="1" cols="30" name="parameters">{}</textarea>
<p class="help">Enter a valid Python Dictionary</p>
</div>
</div>
And here is a screentshot:
这是一个截图:
According to W3C referecnce for text area:
根据W3C 对文本区域的引用:
The size of a textarea can also be specified by the CSS height and width properties.
textarea 的大小也可以由 CSS 的高度和宽度属性指定。
So, my questions are:
所以,我的问题是:
- Is django's own css theme is the responsible here for the "odd" behavior of this widget?
- Can some suggest a way to solve this issue?
- django 自己的 css 主题是这里小部件的“奇怪”行为的原因吗?
- 有人可以提出解决这个问题的方法吗?
采纳答案by alecxe
This is a browser-specific problem.
这是浏览器特定的问题。
According to the thread Height of textarea does not match the rows in Firefox:
根据线程Height of textarea 与 Firefox 中的行不匹配:
Firefox always adds an extra line after the textfield. If you want it to have a constant height, use CSS ...
Firefox 总是在文本字段后添加额外的一行。如果您希望它具有恒定的高度,请使用 CSS ...
You can set a style
attribute of the textarea:
您可以设置style
textarea的属性:
from django.db import models
from django.forms import Textarea
class RulesAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': Textarea(
attrs={'rows': 1,
'cols': 40,
'style': 'height: 1em;'})},
}
Works for me - tested on Firefox v. 23 and Chrome v. 29.
对我有用 - 在 Firefox v. 23 和 Chrome v. 29 上测试。
Hope that helps.
希望有帮助。