【问题标题】:Customizing each row of the model in admin page在管理页面中自定义模型的每一行
【发布时间】:2020-07-15 05:10:16
【问题描述】:

我正在自定义 django 管理模板。

我可以通过更改覆盖change_list_results.htmlchange_list.html 成功删除按钮(+添加模型)或某些过滤器

但现在我想自定义模型的每一行以关闭链接。(我不想让用户去每一行编辑页面。)

我正在检查change_list_result.html

{% load i18n static %}
{% if result_hidden_fields %}
<div class="hiddenfields">{# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}
{% if results %}
<div class="results">
<table id="result_list">
<thead>
<tr> 
{% for header in result_headers %}
<th scope="col" {{ header.class_attrib }}>
   {% if header.sortable %}
     {% if header.sort_priority > 0 %}
       <div class="sortoptions">
         <a class="sortremove" href="{{ header.url_remove }}" title="{% trans "Remove from sorting" %}"></a>
         {% if num_sorted_fields > 1 %}<span class="sortpriority" title="{% blocktrans with priority_number=header.sort_priority %}Sorting priority: {{ priority_number }}{% endblocktrans %}">{{ header.sort_priority }}</span>{% endif %}
         <a href="{{ header.url_toggle }}" class="toggle {% if header.ascending %}ascending{% else %}descending{% endif %}" title="{% trans "Toggle sorting" %}"></a>
       </div>
     {% endif %}
   {% endif %}
   <div class="text">{% if header.sortable %}<a href="{{ header.url_primary }}">{{ header.text|capfirst }}</a>{% else %}<span>{{ header.text|capfirst }}</span>{% endif %}</div>
   <div class="clear"></div>
</th>{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
    <tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}

发现&lt;tr class="{% cycle 'row1' 'row2' %}"&gt;{% for item in result %}{{ item }}{% endfor %}&lt;/tr&gt;在每一行中。

但是如何自定义每个 item ???

感谢您的帮助。

【问题讨论】:

    标签: python django


    【解决方案1】:

    在 django 1.7+ 中,您可以从管理模型的列表中删除链接:

    class UsersAdmin(admin.ModelAdmin):
        list_display_links = None
    

    但是,请注意,这只会删除链接 - 如果用户能够提供相关的 url,它不会阻止用户进入每一行的查看/编辑页面。为此,您还需要处理该视图。

    在此处查看其中一些讨论:https://stackoverflow.com/a/5837386/3121897

    【讨论】:

    • 谢谢!!它击中了我的位置!
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 2011-12-28
    • 2013-03-08
    • 2020-03-10
    • 2014-09-15
    • 2011-11-22
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多