在管理站点中创建隐藏字段

发布于 2024-10-17 04:37:34 字数 278 浏览 3 评论 0原文

如何在管理站点中创建完全隐藏的字段(输入和标签)? 我知道 exclude 属性,但它完全从模板中排除该字段,而我在网页中需要它,但隐藏:

class OutForm(ModelForm):
    reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), widget=forms.HiddenInput)

在管理模板中,我实际上可以隐藏一个字段,但不能隐藏它标签。

How can I create a fully hidden field (input and label) in the admin site?
I know about the exclude property, but it fully excludes the field from the template, while I need it in the web page, but hidden:

class OutForm(ModelForm):
    reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), widget=forms.HiddenInput)

In the admin template I actually can hide a field, but not its label.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你在我安 2024-10-24 04:37:34

Django 管理员尚不支持隐藏字段。有一个开放的票证: https://code.djangoproject.com/ticket/11277

但是,有一些解决方法不需要 jQuery。管理表单使用admin/includes/fieldset.html呈现。如果您覆盖此模板,则可以注入一个 CSS 类来表示要隐藏的行:

<div class="form-row
    {% if line.fields|length_is:'1' and line.errors %} errors{% endif %}
    {% for field in line %} {{ field.field.name }}
      {% if field.field.is_hidden %} has-hidden-field{% endif %}  # add this part
    {% endfor %}">

这实际上是文件中的一行,我已对其进行了扩展以使其更具可读性。

(整洁的细节:对于 StackedInline/TabularInline 对象,您可以在 Python 代码中将模板指定为变量)

接下来,您可以在 CSS 中隐藏该行:

.form-row.has-hidden-field {
    display: none;
}

您可以通过管理页面加载:

{% block extrastyle %}{{ block.super }}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}mysite/admin.css" />{% endblock %}

或使用 modeladmin 中的媒体定义:

class Media:
    css = {'all': ('mysite/admin.css',)

The Django admin does not support hidden fields yet. There is an open ticket for that: https://code.djangoproject.com/ticket/11277

However, there are workarounds that don't require jQuery. The admin forms are rendered using admin/includes/fieldset.html. If you override this template, you can inject a CSS class to denote the row for hiding:

<div class="form-row
    {% if line.fields|length_is:'1' and line.errors %} errors{% endif %}
    {% for field in line %} {{ field.field.name }}
      {% if field.field.is_hidden %} has-hidden-field{% endif %}  # add this part
    {% endfor %}">

this is actually a single line in the file, I've expanded it to make it more readable.

( Neat detail: for an StackedInline/TabularInline objects, you can specify the template as variable in Python code. )

Next, you can hide that row in your CSS:

.form-row.has-hidden-field {
    display: none;
}

Which you can load via your admin page:

{% block extrastyle %}{{ block.super }}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}mysite/admin.css" />{% endblock %}

or by using the media definition in the modeladmin:

class Media:
    css = {'all': ('mysite/admin.css',)
青柠芒果 2024-10-24 04:37:34

在模板中,显示时

{% for field in form.visible_fields %}
    {{ field.label_tag }} : {{ field }}
{% endfor %}

会隐藏隐藏字段标签。

In the template, while displaying

{% for field in form.visible_fields %}
    {{ field.label_tag }} : {{ field }}
{% endfor %}

It will hide the hidden field labels.

捎一片雪花 2024-10-24 04:37:34

我发现在字段集中使用“classes”属性来隐藏字段非常方便,但仍将它们保留在请求中。在你的模型管理中你可以写

fieldsets = [
        ('Visible Fields', 
         {'fields': [('name', 'comment'),]}),
        ('Collapsable Fields', 
         {'fields': [('rare_property',)],'classes': ['collapse']}),
        ('Hidden Fields', 
         {'fields': [('magic_property',)],'classes': ['hidden']}),
            ]

I found it quite handy to use the 'classes' attribute in fieldsets to hide fields but still leave them in a request. In your model admin you can write

fieldsets = [
        ('Visible Fields', 
         {'fields': [('name', 'comment'),]}),
        ('Collapsable Fields', 
         {'fields': [('rare_property',)],'classes': ['collapse']}),
        ('Hidden Fields', 
         {'fields': [('magic_property',)],'classes': ['hidden']}),
            ]
乖乖公主 2024-10-24 04:37:34

您在示例中给出的是 ModelForm,而不是您应该用于管理站点的 ModelAdmin。

无论如何,排除某些字段的方法是相同的:在 exclude 属性中指定它:

class OutForm(ModelForm):
  class Meta:
    exclude = ["reply_to"]

class OutAdmin(ModelAdmin):
  exclude = ["reply_to"]

参阅 Django 文档了解详细信息:http://docs.djangoproject.com/en/1.2/ref/contrib/admin/

You're giving ModelForm in your example, not ModelAdmin you should be using for admin site.

Anyway, method of excluding some field is the same: specify it in exclude property:

class OutForm(ModelForm):
  class Meta:
    exclude = ["reply_to"]

or

class OutAdmin(ModelAdmin):
  exclude = ["reply_to"]

See Django documentation for details: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/

·深蓝 2024-10-24 04:37:34

尝试将 label="" 添加到 ModelChoiceField 以使标签成为空字符串:

reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), label="", widget=forms.HiddenInput)

Try adding label="" to your ModelChoiceField to make the label an empty string:

reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), label="", widget=forms.HiddenInput)
∝单色的世界 2024-10-24 04:37:34

看起来 Django 3.2 中的 HiddenInput 小部件足以隐藏表单中的整行。我已经通过自定义管理表单和 formfield_override 属性尝试过此操作。包含隐藏字段的类“form-row”的 div 获得附加类“hidden”,并且不显示。

It seems that in Django 3.2 the HiddenInput widget is enough to hide the entire row from the form. I have tried this both via custom admin form and via the formfield_override property. The div with class "form-row" containing the hidden field gets additional class "hidden" and is not shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文