如何在Django Admin中隐藏HiddenInput小部件的字段标签? [英] How do I hide the field label for a HiddenInput widget in Django Admin?
问题描述
我有一些Django表单代码,如下所示:
class GalleryAdminForm(forms.ModelForm):
auto_id = False
order = forms.CharField(widget = forms.HiddenInput())
这使得表单字段消失,但在Django管理页面中留下标签Order。如果我使用:
order = forms.CharField(widget = forms.HiddenInput(),label ='')
/ / code>
我还剩下字段和标签之间的:。
如何隐藏整个事情?
如果您使用JQuery这应该是诀窍:
您的表单
code> TO_HIDE_ATTRS = {'class':'hidden'}
class GalleryAdminForm(forms.ModelForm):
auto_id = False
order = forms.CharField(widget = forms.TextInput (attrs = TO_HIDE_ATTRS))
添加到您的模板的Javascript代码 / p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ .hide();
});
如果您将表单呈现为表格,则可以正常工作。如果您希望使用任何形式的渲染工作,您可以执行以下操作:
$(document).ready function(){
$('{{form_field_container}}:has(.hidden)')。hide();
});
并将 form_field_container
添加到您的模板上下文中。一个例子:
如果您的表单如下所示:
<形式>
< span> {{field.label_tag}} {{field}}< / span>
< / form>
您的上下文必须包括:
'form_field_container':'span'
。
I've got a bit of Django form code that looks like this:
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:
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?!
If you're using JQuery this should do the trick:
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
$(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:
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...
这篇关于如何在Django Admin中隐藏HiddenInput小部件的字段标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!