简体   繁体   English

如何仅为一个管理表单覆盖`base.html` Django admin?

[英]How to override `base.html` Django admin only for one admin form?

I have a model that will be used for getting data from user.我有一个用于从用户获取数据的模型。 There will be only one input field and one button for submitting.将只有一个输入字段和一个提交按钮。 Therefor i want to remove other buttons.因此我想删除其他按钮。

Let's say you have a model as class Post(models.Model):假设您有一个模型作为class Post(models.Model):

In your admin.py file create something like this:在您的 admin.py 文件中创建如下内容:

class Post(models.Model):
    title = ...
    

    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        if object_id:
            extra_context.update(
                instance=Posts.objects.get(pk=object_id)
            )
        return super().change_view(
            request, object_id, form_url=form_url, extra_context=extra_context
        )

So that whenever you go to your Posts page in the admin you will get the variable {{instance}}因此,无论何时您在管理员中进入您的帖子页面,您都会获得变量{{instance}}

Then you should create a new folder inside your templates folder and call it admin then inside admin folder create a new file called change_form.html (names are important) docs inside that file:那么你应该里面你创建一个新的文件夹templates文件夹,并把它admin那里面admin文件夹中创建一个名为新文件change_form.html (名称是重要) 的文档该文件中:

{% extends "admin/change_form.html" %}
{% block after_field_sets %}
{% if instance %}
   PUT YOUR CHANGES IN HERE
{% endif %}
{% endblock %}

This will override only Post page in the admin.这将仅覆盖管理员中的 Post 页面。 Other pages will stay the same.其他页面将保持不变。 To decide exactly where to put your changes, you can check the default change_form.html of django.要确定将更改放在哪里,您可以检查 django 的默认change_form.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM