python 如何在 Django Admin 中隐藏 HiddenInput 小部件的字段标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408940/
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
How do I hide the field label for a HiddenInput widget in Django Admin?
提问by Gabriel Hurley
I've got a bit of Django form code that looks like this:
我有一些 Django 表单代码,如下所示:
class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:
这使得表单字段消失,但它在 Django 管理页面中留下了“订单”标签。如果我使用:
order = forms.CharField(widget=forms.HiddenInput(), label='')
I'm still left with the ":" between where the field and label used to be.
我仍然在字段和标签曾经所在的位置之间留下“:”。
How do I hide the whole thing?!
我如何隐藏整个事情?!
采纳答案by Guillem Gelabert
If you're using JQuery this should do the trick:
如果您使用的是 JQuery,这应该可以解决问题:
Your form
你的表格
TO_HIDE_ATTRS = {'class': 'hidden'}
class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.TextInput(attrs=TO_HIDE_ATTRS))
Javascript code to add to your template
要添加到模板的 Javascript 代码
$(document).ready(function(){
$('tr:has(.hidden)').hide();
});
That works if you're rendering your form as a table. If you want to make it work with any kind of form rendering you can do as follows:
如果您将表单呈现为表格,这会起作用。如果您想让它与任何类型的表单渲染一起使用,您可以执行以下操作:
$(document).ready(function(){
$('{{ form_field_container }}:has(.hidden)').hide();
});
And add form_field_container
to your template context. An example:
并添加form_field_container
到您的模板上下文中。一个例子:
If you render your form like this:
如果您像这样渲染表单:
<form>
<span>{{ field.label_tag }} {{ field }}</span>
</form>
Your context must include:
您的上下文必须包括:
'form_field_container': 'span'
You get the idea...
你明白了...
回答by emispowder
Oraculum has got it right. You shouldn't be cleaning this up on the client side. If it is clutter, then you shouldn't be sending it to client at all. Building on Oraculum's answer, you should use a custom form template because you you probably still want the hidden values in the form.
Oraculum 做对了。你不应该在客户端清理它。如果它很杂乱,那么您根本不应该将其发送给客户。基于 Oraculum 的答案,您应该使用自定义表单模板,因为您可能仍然需要表单中的隐藏值。
{% for field in form.visible_fields %}
<div>
{{ field.errors }}
<span class="filter-label">{{ field.label_tag }}</span><br>
{{ field }}
</div>
{% endfor %}
{% for field in form.hidden_fields %}
<div style="display:none;">{{ field }}</div>
{% endfor %}
Using a custom form template to control hidden fields is cleaner because it doesn't send extraneous info to the client.
使用自定义表单模板来控制隐藏字段更清晰,因为它不会向客户端发送无关信息。
回答by Anentropic
I can't believe several people have suggested using jQuery for this...
我不敢相信有几个人建议为此使用 jQuery...
Is it a case of: when the only tool you know is a hammer everything looks like a nail?
是否是这样的:当你知道的唯一工具是锤子时,一切看起来都像钉子?
Come on, if you're going to do it from the client-side (instead of fixing the source of the problem in the back-end code) surely the right place to do it would be in CSS?
来吧,如果您打算从客户端执行此操作(而不是在后端代码中修复问题的根源),那么在 CSS 中执行此操作的正确位置肯定是?
If you're in the admin site then it's a bit harder but if it's a regular page then it's easy to just omit the whole label from the form template, for example
如果您在管理站点中,那么它会有点困难,但如果它是一个常规页面,那么很容易从表单模板中省略整个标签,例如
If you're in the admin site then you could still override the as_table, as_ul, as_p methods of BaseForm (see django/forms/forms.py) in your GalleryAdminForm class to omit the label of any field where the label is blank (or == ':' as the value may be at this stage of rendering)
如果您在管理站点中,那么您仍然可以覆盖 GalleryAdminForm 类中 BaseForm 的 as_table、as_ul、as_p 方法(请参阅 django/forms/forms.py)以省略标签为空白的任何字段的标签(或== ':' 因为该值可能处于渲染的这个阶段)
(...looking at lines 160-170 of forms.pyit seems like Django 1.2 should properly omit the ':' if the label is blank so I guess you're on an older version?)
(...查看forms.py 的第 160-170行,似乎 Django 1.2 应该正确地省略 ':' 如果标签为空白,所以我猜您使用的是旧版本?)
回答by Damon Abdiel
Try
尝试
{% for field in form.visible_fields %}
{% for field in form.visible_fields %}
回答by vdboor
Check the answer at Create a hidden field in the admin site, it can be done without JavaScript by overriding admin/includes/fieldset.html
From there, you can inject a CSS class, and do the rest.
检查在管理站点中创建隐藏字段的答案,它可以通过覆盖admin/includes/fieldset.html
在没有 JavaScript 的情况下完成从那里,您可以注入 CSS 类,然后完成其余的工作。
回答by Tonino
I think it's simpler to achieve the ":" label omission for HiddenInput widget by modifying class AdminField(object)
in contrib/admin/helpers.py
from :
我认为这是更简单的实现:通过修改HiddenInput插件标签不作为“”class AdminField(object)
在contrib/admin/helpers.py
从:
if self.is_checkbox:
classes.append(u'vCheckboxLabel')
contents = force_unicode(escape(self.field.label))
else:
contents = force_unicode(escape(self.field.label)) + u':'
to :
到 :
if self.is_checkbox:
classes.append(u'vCheckboxLabel')
contents = force_unicode(escape(self.field.label))
else:
contents = force_unicode(escape(self.field.label))
#MODIFIED 26/10/2009
if self.field.label <> '':
contents += u':'
# END MODIFY
回答by Wilfred Hughes
In theory, you should be able to pass label_suffix
into the form constructor. However, the Django admin ignores this.
理论上,您应该能够传入label_suffix
表单构造函数。但是,Django 管理员忽略了这一点。
You've been bitten by two bugs in Django: #18134 'BoundField.label_tag should include form.label_suffix'(fixed in trunk, should be in 1.6) and to a lesser extent #11277 Hidden fields in Inlines are displayed as empty rows.
您已经被 Django 中的两个错误所困扰:#18134 'BoundField.label_tag 应该包括 form.label_suffix'(在主干中修复,应该在 1.6 中)并且在较小程度上#11277 Inline 中的隐藏字段显示为空行。
Currently, the best solution is to override the admin fieldset template. Use a HiddenInput
for your widget, then override the admin fieldset template (documented here). Just create a templates/admin/includes/fieldset.html
with the following contents:
目前,最好的解决方案是覆盖管理字段集模板。HiddenInput
为您的小部件使用 a ,然后覆盖管理字段集模板(此处记录)。只需templates/admin/includes/fieldset.html
使用以下内容创建一个:
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}
<div class="description">{{ fieldset.description|safe }}</div>
{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
{% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
{% for field in line %}
<div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}>
{% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{# only show the label for visible fields #}
{% if not field.field.is_hidden %}
{{ field.label_tag }}
{% endif %}
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{{ field.field }}
{% endif %}
{% endif %}
{% if field.field.help_text %}
<p class="help">{{ field.field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
回答by lacki
Based upon the solution by Wilfried Hughes I 've changed the fieldset.html with little improvements.
根据 Wilfried Hughes 的解决方案,我更改了 fieldset.html,但几乎没有改进。
The code snippet below not only hides the input element instead if the fieldset contains only one single element which input-type is set to hidden it also hides the surrounding div-elements wasting no space in the form.
下面的代码片段不仅隐藏输入元素,如果字段集只包含一个输入类型设置为隐藏的单个元素,它还会隐藏周围的 div 元素,不会在表单中浪费空间。
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}
<div class="description">{{ fieldset.description|safe }}</div>
{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}"{% if line.fields|length_is:'1' %}{% for field in line %}{% if field.field.is_hidden %} style="display: none"{% endif %}{% endfor %}{% endif %}>
{% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
{% for field in line %}
<div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}{% if field.field.is_hidden %} style="display: none"{% endif %}>
{% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{# only show the label for visible fields #}
{% if not field.field.is_hidden %}
{{ field.label_tag }}
{% endif %}
{% if field.is_readonly %}
<p>{{ field.contents }}</p>
{% else %}
{{ field.field }}
{% endif %}
{% endif %}
{% if field.field.help_text %}
<p class="help">{{ field.field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
回答by SarahJaine
The following removes the ':' from all your form fields. I've only tried it with the forms.Form
class, but I believe it should work for forms.ModelForm
too.
以下内容从您的所有表单字段中删除了“:”。我只在forms.Form
课堂上尝试过,但我相信它也应该适用forms.ModelForm
。
In Django forms, the ':' after the labels is the label_suffix
. You can change or remove the label_suffix
by creating a subclass of ModelForm
, here called UnstyledForm
, and redefining the initialization function with label_suffix
set to an empty string. Then use your new UnstyledForm
class.
在 Django 表单中,标签后的 ':' 是label_suffix
. 您可以label_suffix
通过创建 的子类(ModelForm
此处称为UnstyledForm
)并使用label_suffix
set 为空字符串重新定义初始化函数来更改或删除。然后使用您的新UnstyledForm
课程。
class UnstyledForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
kwargs.setdefault('label_suffix', '')
super(UnstyledForm, self).__init__(*args, **kwargs)
class GalleryAdminForm(UnstyledForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
I hope that helps!
我希望这有帮助!
回答by Rafa He So
Another way to do it, but i think it still better to iterate form.visible_fields & form.hidden_fields
另一种方法,但我认为最好还是迭代 form.visible_fields & form.hidden_fields
<form action="{% url 'some_url' param %}" method="POST">
{% csrf_token %}
<div class="row">
{% for field in form %}
{% if not field.is_hidden %}
<div class="col-md-6">
{{ field.label_tag }}
{{ field.error }}
{{ field }}
</div>
{% else %}
{{ field }}
{% endif %}
{% endfor %}
</div>
</form>