Django - 管理 - 带有 ' 的必填字段* '

发布于 2024-10-10 12:49:26 字数 58 浏览 0 评论 0原文

目前,Django admin 将以粗体标签显示所有必填字段。是否可以在标签中用*标记而不是粗体标签?

At present, Django admin will show all the mandatory fields with a bold labels. Is it possible mark with * in the label instead of bold labels?

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

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

发布评论

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

评论(1

烟雨扶苏 2024-10-17 12:49:26

Django 管理员使用模板来呈现模型的添加/编辑页面。可以将该模板替换为您自己的模板(从原始模板扩展),覆盖您需要的模板块,以便进行您想要的更改。

查看有关覆盖管理模板的 Django 文档 了解更多信息。

这是 admin/change_form.html 模板< /a> 您需要以某种方式进行更改(因为此模板会呈现当您添加新实例或编辑现有实例时显示的页面)。现有模板已经将 required 类应用于适当的标签,因此我将创建一个如下所示的新模板:

{% extends "admin/change_form.html" %}

{% block extrastyle %}
    {{ block.super }}
    <style type="text/css">
        /* add an asterisk using CSS */
        .required:after {
            content: " *";
        }
    </style>
{% endblock %}

应用于单个模型

您应该使用 模型管理类 如果您希望此模板用于特定模型,请设置 change_form_template 属性,如 本节所述将 docs 复制到您创建的模板文件的位置。

应用于单个应用程序

如果您希望模板应用于整个应用程序中的模型,请在应用程序的根目录中创建一个 templates 文件夹。 Django 将自动在那里查找模板,所以如果您创建一个名为 admin 的文件夹,并在其中放置一个名为 change_form.html 的文件,它将自动覆盖该名称的默认 Django 模板 (admin/change_form.html )。

项目范围

为了在项目范围内应用此模板,请在某处(不在应用程序内)创建一个名为 templates 的文件夹。再次将新模板放入 admin/change_form.html 目录中。

接下来 编辑模板目录 Django 设置 指定此目录的位置,以便 Django 能够找到模板并以与之前相同的方式覆盖默认模板,仅适用于项目范围,而不仅仅是应用程序范围。


这是一组相当复杂的事情,特别是对于这样一个简单的更改,如果您以前没有使用过管理模板(或者即使您使用过),您可能会发现它很棘手。

希望您现在了解更改管理模板所需的内容,它实际上相当优雅(就像 Django 一样),但在我看来,仅仅更改为某些星号并不值得付出努力。

The Django admin uses templates to render the add/edit page for a model. It is possible to replace that template with one of your own (which extends from the original template) overriding the template blocks you need to in order to make the changes you want to.

Check out the Django docs regarding overriding admin templates for more information.

It's the admin/change_form.html template which you would need to alter in some way (since this template renders the page shown when you add a new instance or edit an existing one). The existing templates already apply a required class to the appropriate labels, so I would create a new template which looks like this:

{% extends "admin/change_form.html" %}

{% block extrastyle %}
    {{ block.super }}
    <style type="text/css">
        /* add an asterisk using CSS */
        .required:after {
            content: " *";
        }
    </style>
{% endblock %}

Apply to a Single Model

You should use a model admin class if you want this template to be used for specific models, setting the change_form_template attribute, as described in this section of the docs to the location of the template file you have created.

Apply to a Single App

If you want template to apply to models in an entire app create a templates folder inside the root of the app. Django will automatically look for templates there, so if you create a folder called admin and place a file in there called change_form.html it will automatically override the default Django template of that name (admin/change_form.html).

Project Wide

In order to apply this template project wide create a folder somewhere (not inside an app) called templates. Again place your new template in this directory at admin/change_form.html.

Next edit the template directories Django setting specifying the location of this directory in order to allow Django to find the template and override the default templates in the same way as before only project wide and not just app wide.


This is quite a complex set of things to do, especially for such a simple change and you may find it tricky if you have not worked with admin templates before (or even if you have).

Hopefully you now understand what is required to change an admin template, its actually fairly elagant (as is Django) but in my opinion not worth the effort just to change to some asterisks.

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