【问题标题】:Remove history button from Django admin从 Django 管理员中删除历史按钮
【发布时间】:2017-11-26 10:40:40
【问题描述】:

我想根据用户类型从 django 管理按钮启用/禁用历史记录。

我的最终目标是能够了解如何显示隐藏此按钮。

【问题讨论】:

    标签: python django django-admin django-simple-history


    【解决方案1】:

    不幸的是,Django 没有提供一种简单的方法来切换历史按钮,就像它为“添加”按钮所做的那样。最简单的方法是覆盖change_form.html 并从block object-tools-items 中删除下一行:

    <li>
            {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
            <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
    </li>
    

    请记住,您必须为每个管理模型指定 change_form。 示例:

    class TestAdmin(admin.ModelAdmin):
        # path to the app_name/templates/admin/app_name/change_form.html
        change_form_template = 'admin/app_name/change_form.html'
    
    # Register your models here.
    admin.site.register(Test, TestAdmin)
    

    【讨论】:

    • 您可以只覆盖{% block object-tools-items %},而不是覆盖整个模板,尽管它也有“绝对网址”部分。
    【解决方案2】:

    一个干净的解决方案是覆盖change_form_object_tools.html template,它需要放在您项目的templates/admin/ 中。

        {% load i18n admin_urls %}
        {% block object-tools-items %}
    
        {% block comment %}
         <li>
            {% url opts|admin_urlname:'history' original.pk|admin_urlquote as   history_url %}
            <a href="{% add_preserved_filters history_url %}" class="historylink">
     {% translate "History" %}</a>
        </li>
        {% endcomment %}
    
        {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% translate "View on site" %}</a></li>{% endif %}
        {% endblock %}
    

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多