Job Search Engine


963 浏览 5 years, 8 months

2.7 列表视图显示优化

版权声明: 转载请注明出处 http://www.codingsoho.com/

列表视图显示优化

我们要解决列表显示中的几个问题

1.表头的内容直接从对象中读取,而不是手动写死

这个值可以直接从verbose_name获取

添加templatetag myform.py,这个是我之前写好的,这儿直接引用

from django import template
from django.db import models
from django.utils.translation import ugettext as _

register = template.Library()

def get_model_item(inst, fieldname):
    if not fieldname.find("__") == -1: # only one __ allowed
        modelname, fieldname2 = fieldname.split("__")
        inst = getattr(inst, modelname)
        fieldname = fieldname2
    return inst, fieldname

@register.filter(name='my_isboolean')
def my_isboolean(inst, fieldname):
    if hasattr(inst, fieldname):
        field = inst._meta.get_field(fieldname)   
        if isinstance(field, models.BooleanField):
            return True
    return False

@register.filter(name='my_get_field_verbose_name')
def my_get_field_verbose_name(inst, fieldname):
    inst, fieldname = get_model_item(inst, fieldname)        
    if hasattr(inst, fieldname):
        field = inst._meta.get_field(fieldname)
        return field.verbose_name
    return None

@register.filter(name='my_get_field_value')
def my_get_field_value(inst, fieldname):
    inst, fieldname = get_model_item(inst, fieldname)        
    if hasattr(inst, fieldname):
        value = getattr(inst, fieldname)
        if None == value:
            value = ""
        else:
            pass
        return value
    return None

@register.filter(name='my_get_field_display')
def my_get_field_display(inst, fieldname):
    inst, fieldname = get_model_item(inst, fieldname)        
    if hasattr(inst, fieldname):
        field = inst._meta.get_field(fieldname)   
        value = getattr(inst, fieldname)
        if True == value and my_isboolean(inst, fieldname):
            return  _("Yes")
        elif False == value and my_isboolean(inst, fieldname):
            return  _("No")
        elif None == value:
            return  ""
        else:
            pass
        return "%s" % inst._get_FIELD_display(field)  
    return None

修改jobentry_list.html

{% load staticfiles i18n myform %}

{% block content %}

<table class="table table-striped table-responsive table-condensed table-hover table-bordered">
  <caption>Job Entry ListView</caption>
  <thead>
    <tr>
      {% for fieldname in fields_display %}
      <th class="{{fieldname}}">{{ object_list.0|my_get_field_verbose_name:fieldname }}</th>
      {% endfor %}
      <th class="href">Link</th>
    </tr>
  </thead>
  <tbody>
    {% for obj in object_list %}
    <tr>
      {% for fieldname in fields_display %}
      {% if forloop.first %}
      <td class="{{fieldname}}"><a href="{{obj.get_absolute_url}}">{{ obj|my_get_field_display:fieldname }}</a> </td>
      {% else %}      
      <td class="{{fieldname}}">{{ obj|my_get_field_value:fieldname }}</td>
      {% endif %}
      {% endfor %}
      <td class="href"><a href="{{ obj.href }}">href</a></td>
    </tr>
    {% endfor %}
  </tbody>
</table>
{% endblock %}

上面代码中,标题用my_get_field_verbose_name获取,值用my_get_field_value获取。

变量fields_display需要在context中传递过来

class JobEntryListView(ListView):
    model = JobEntry

    def get_context_data(self, *args, **kwargs):
        context = super(JobEntryListView, self).get_context_data(*args, **kwargs)
        fields_display = [
            'title',
            'salary',
            'region',
            'degree',
            'experience',
            'company',
            'industry',
            'description'
        ]
        context['fields_display'] = fields_display
        return context

因为标题内容可以灵活定制,可以去除不重要的内容,这样表格的显示明显紧凑了,但是还不够好,因为列宽是自动的,不能到达最有

2.调整列宽

在base.html添加block

  <style>
    {% block style %}{% endblock %} 
  </style>  

在jobentry_list.html定义css

<style>
{% block style %}
table th.title,
table th.company,
table th.industry{
    width: 120px;
}

table th.salary,
table th.region,
table th.degree,
table th.experience{
    width: 60px;
}
{% endblock %} 
</style>  

表格里的class在前面创建表格时已生成。

当然我们也可以用其他方法,比如nth-child()等,实践下来还是当前使用方法灵活