【问题标题】:Django - modify css in result list itemDjango - 在结果列表项中修改 css
【发布时间】:2018-03-09 22:46:57
【问题描述】:

我的管理面板中有简单的结果列表。例如它是这样的:

id名称描述类别

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

if obj.group_id == 2:
    add class "premium" to result list item  # whole row with this item

我想在我的 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"

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

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    Django 不会让你所问的事情变得容易......

    首先你需要创建一个新的template_tags:
    我将文件称为 admin_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_tag返回一个dict列表而不是一个ResultList列表,并将渲染模板更改为“admin/change_list_results_with_class.html”。

    然后你需要像 answer

    那样覆盖 change_list.html
    {% extends "admin/change_list.html" %}
    {% load i18n admin_urls static admin_list admin_override%}
    
    {% block result_list %}
        {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
        {% result_list_with_class cl %}
        {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
    {% endblock %}
    

    最后你创建 admin/change_list_results_with_class.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.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 %}
    

    您在您的模型中添加一个函数:

    @property
    def admin_css_class(self):
        return 'my_class'
    

    这是经过测试和工作的。

    【讨论】:

    • {{ item.admin_css_class }} 在这里不起作用。在下一个循环中调用项目...我添加了您编写的函数,但我不知道如何在模板中使用它...
    • 你说得对,我没有遵循 pep20 “面对歧义,拒绝猜测的诱惑。”。我会编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2010-09-30
    • 2018-12-17
    相关资源
    最近更新 更多