Django-在结果列表项中修改css

Django-在结果列表项中修改css,django,django-admin,Django,Django Admin,我的管理面板中有一个简单的结果列表。例如: id名称描述类别 在我的管理面板中,有一个结果列表,其中包含“row1”、“row2”类。我想根据obj值(未列出的obj.group_id)添加另一个类。我的意思是: 我只想在django管理面板中区分一些行。 我不知道如何才能做到这一点。有什么线索吗 我能够在一列上更改css。例如: def show_url(self, obj): if obj.group.group_name == 'Premium': return f

我的管理面板中有一个简单的结果列表。例如:

id名称描述类别

在我的管理面板中,有一个结果列表,其中包含“row1”、“row2”类。我想根据obj值(未列出的obj.group_id)添加另一个类。我的意思是:

我只想在django管理面板中区分一些行。 我不知道如何才能做到这一点。有什么线索吗

我能够在一列上更改css。例如:

def show_url(self, obj):
    if obj.group.group_name == 'Premium':
        return format_html(
            '<span class="premium"><a style="color:blue" href="{}">{}</a></span>'.format(obj.url, obj.url)
        )
    return '<a style="color:blue" href="{}">{}</a>'.format(obj.url, obj.url)
show_url.allow_tags = True
show_url.short_description = "Url"
def show_url(self,obj):
如果obj.group.group_name=='Premium':
返回格式(
''.format(obj.url,obj.url)
)
返回“”。格式(obj.url,obj.url)
show_url.allow_tags=True
show_url.short_description=“url”

我想为整行设置不同的背景…

Django不会让你的问题变得简单

首先,您需要创建一个新模板。\u标签:
我调用了文件admin\u override.py

# -*- coding: utf-8 -*-

from django.contrib.admin.templatetags.admin_list import (
    ResultList, items_for_result,
    result_headers, result_hidden_fields
)

from django.template import Library
register = Library()


def results(cl):
    if cl.formset:
        for res, form in zip(cl.result_list, cl.formset.forms):
            yield ResultList(form, items_for_result(cl, res, form))
    else:
        for res in cl.result_list:
            yield {
                'class': res.admin_css_class,
                'items': ResultList(None, items_for_result(cl, res, None))
            }


@register.inclusion_tag("admin/change_list_results_with_class.html")
def result_list_with_class(cl):
    """
    Displays the headers and data list together
    """
    headers = list(result_headers(cl))
    num_sorted_fields = 0
    for h in headers:
        if h['sortable'] and h['sorted']:
            num_sorted_fields += 1
    return {'cl': cl,
            'result_hidden_fields': list(result_hidden_fields(cl)),
            'result_headers': headers,
            'num_sorted_fields': num_sorted_fields,
            'results': list(results(cl))}
这个新的template_标记返回一个dict列表而不是ResultList列表,并将呈现模板更改为“admin/change_list_results_with_class.html”

然后您需要像下面这样覆盖change_list.html

最后,您可以使用class.html创建管理/更改列表\u结果\u

{% 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.non_field_errors %}
    <tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} {{ result.class }}">{% for item in result.items %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}

这是经过测试且有效的。

Django不会让你所要求的任务变得简单

首先,您需要创建一个新模板。\u标签:
我调用了文件admin\u override.py

# -*- coding: utf-8 -*-

from django.contrib.admin.templatetags.admin_list import (
    ResultList, items_for_result,
    result_headers, result_hidden_fields
)

from django.template import Library
register = Library()


def results(cl):
    if cl.formset:
        for res, form in zip(cl.result_list, cl.formset.forms):
            yield ResultList(form, items_for_result(cl, res, form))
    else:
        for res in cl.result_list:
            yield {
                'class': res.admin_css_class,
                'items': ResultList(None, items_for_result(cl, res, None))
            }


@register.inclusion_tag("admin/change_list_results_with_class.html")
def result_list_with_class(cl):
    """
    Displays the headers and data list together
    """
    headers = list(result_headers(cl))
    num_sorted_fields = 0
    for h in headers:
        if h['sortable'] and h['sorted']:
            num_sorted_fields += 1
    return {'cl': cl,
            'result_hidden_fields': list(result_hidden_fields(cl)),
            'result_headers': headers,
            'num_sorted_fields': num_sorted_fields,
            'results': list(results(cl))}
这个新的template_标记返回一个dict列表而不是ResultList列表,并将呈现模板更改为“admin/change_list_results_with_class.html”

然后您需要像下面这样覆盖change_list.html

最后,您可以使用class.html创建管理/更改列表\u结果\u

{% 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.non_field_errors %}
    <tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} {{ result.class }}">{% for item in result.items %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}

这是经过测试的,可以正常工作。

{{item.admin\u css\u class}}在这里不起作用。项在下一个循环中被调用。。。我在你写的时候添加了函数,但我不知道如何在模板中使用它……你是对的,我没有遵循pep20“面对歧义,拒绝猜测的诱惑。”。我将编辑我的答案{{item.admin\u css\u class}}在这里不起作用。项在下一个循环中被调用。。。我在你写的时候添加了函数,但我不知道如何在模板中使用它……你是对的,我没有遵循pep20“面对歧义,拒绝猜测的诱惑。”。我将编辑我的答案
@property
def admin_css_class(self):
    return 'my_class'