检查列值并操纵css djangotables2

时间:2014-07-27 08:03:02

标签: django django-templates django-tables2

我第一次使用djangotables2。我已经成功地展示了一张桌子 是一个dictonaries列表,其中键和值是从我的各种来源计算的 项目数据库。

我有一个要求,我必须检查列的值并更改背景颜色,如css或应用bootstrap徽章。

示例代码: -

views.py

data_list = []
#search
query = request.GET.get('q')
if query:
    user_list = UserProfile.objects.filter(user__email__icontains=query)
else:
    user_list = UserProfile.objects.order_by("-user__date_joined")
for userprofile in user_list:
    data_list.append({'user_id': userprofile.id,
                      'first_name': userprofile.user.first_name,
                      'last_name': userprofile.user.last_name,
                      'address': get_city(userprofile),
                      'dateofbirth': userprofile.dateofbirth,
                      'anniversary': "NA",
                      'number_of_orders':
                      get_total_orders(userprofile),
                      'total_revenue': get_total_revenue(userprofile),
                      'highest_order': get_higest_order(userprofile),
                      'registration_date':
                      userprofile.user.date_joined.date().strftime("%B %d %Y"),
                      'last_order_date': get_last_order(userprofile),
                      'last_order_to_present_day_differene':
                      get_difference_from_last_order(userprofile)
                    })
data = ThemedCRMTable(data_list, prefix="3-")
RequestConfig(request, paginate={"per_page": 15}).configure(data)

tables.py

class CRMTable(tables.Table):
    user_id = tables.Column()
    first_name = tables.Column()
    last_name = tables.Column()
    address = tables.Column()
    dateofbirth = tables.Column()
    anniversary = tables.Column()
    number_of_orders = tables.Column()
    total_revenue = tables.Column()
    highest_order = tables.Column()
    registration_date = tables.Column()
    last_order_date = tables.Column()
    last_order_to_present_day_differene = tables.Column()


class ThemedCRMTable(CRMTable):
    class Meta:
        attrs = {'class': 'paleblue'}

模板

{% load static %}
{% load render_table from django_tables2 %}

<!doctype html>
<html>
  <head>
    <title>CRM Report</title>
    <link rel="stylesheet" href="/static/report/css/screen.css" />
    <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
    <script src="{% static 'js/jquery.js' %}"></script>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
  </head>
  <body>
    <div class="container">
        <div class="row" style="margin-top:2%;text-align:center;">
            <form class="form-search pull-left">
              <div class="input-append">
                <input type="text" class="span4 search-query" name="q">
                <button type="submit" class="btn">Search</button>
              </div>
            </form>
            <strong style="font-size:20px;">CRM Report</strong>
            <a class="btn btn-inverse pull-right offset1" href="/">
              <span><i class="icon-step-backward icon-white icon-alignment"></i></span>
              Go Back
            </a>
            <a class="btn btn-success pull-right offset1" href="{% url generate_crm_report_csv %}">
              <span><i class="icon-download icon-white icon-alignment"></i></span>
              CRM Report
            </a>
          </div>
        {% render_table data_list %}
      </div>  
  </body>
</html>

问题: -

我想将单元格背景设为红色,其中last_order_to_present_day_differene的最后一列的值大于30

0 个答案:

没有答案