在管理站点中创建一个隐藏字段 [英] Create a hidden field in the admin site

查看:118
本文介绍了在管理站点中创建一个隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

Django管理员不支持隐藏字段。有一张公开的机票: https://code.djangoproject.com/ticket/11277

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

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

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.

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

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

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

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 %}

或者使用modeladmin中的媒体定义:

or by using the media definition in the modeladmin:

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

这篇关于在管理站点中创建一个隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆