这是代码列表任务并按三个条件 desc 和 asc 对它们进行排序。
我现在有两个问题,那就是:
1)order_by 只应用于第一页。我希望它运行 order_by,然后对整个有序列表进行分页。
2) 从未显示“_pressed”箭头图像。
请帮忙!谢谢!
视图.PY
def task_list(request, **kwargs):
q = Task.objects.all()
if 'sort' in request.GET:
sort_by = request.GET['sort']
else:
sort_by = 'latest-desc'
if sort_by == 'latest-desc':
q = q.order_by('-pub_date')
if sort_by == 'latest-asc':
q = q.order_by('pub_date')
if sort_by == 'price-desc':
q = q.order_by('-price')
if sort_by == 'price-asc':
q = q.order_by('price')
if sort_by == 'deadline-desc':
q = q.order_by('-expiry_date')
if sort_by == 'deadline-asc':
q = q.order_by('expiry_date')
kwargs['queryset'] = q.all()
return list_detail.object_list(request, **kwargs)
网址.PY
urlpatterns = patterns('',
url(r'^tasks/$', 'tasks.views.task_list',
{'template_name':'findtask.html', 'paginate_by':4}, name='tasks'),
)
HTML
<div class="sortList">
<ul>
<li class="sort">Sort by latest
<a href="?sort=latest-desc">{% if request.GET.sort == 'latest-desc' %}<img src="/static/img/downarrow_pressed.gif"/>{% endif %}
{% if request.GET.sort != 'latest-desc' %}<img src="/static/img/downarrow.gif"/>{% endif %}</a>
<a href="?sort=latest-asc">{% if request.GET.sort == 'latest-asc' %}<img src="/static/img/uparrow_pressed.gif"/>{% endif %}
{% if request.GET.sort != 'latest-asc' %}<img src="/static/img/uparrow.gif"/>{% endif %}</a></li>
<li class="sort">Sort by deadline
<a href="?sort=deadline-desc">{% if request.GET.sort == 'deadline-desc' %}<img src="/static/img/downarrow_pressed.gif" />{% endif %}
{% if request.GET.sort != 'deadline-desc' %}<img src="/static/img/downarrow.gif" />{% endif %}</a>
<a href="?sort=deadline-asc">{% if request.GET.sort == 'deadline-asc' %}<img src="/static/img/uparrow_pressed.gif" />{% endif %}
{% if request.GET.sort != 'deadline-asc' %}<img src="/static/img/uparrow.gif" />{% endif %}</a></li>
<li class="sort">Sort by price
<a href="?sort=price-desc">{% if request.GET.sort == 'price-desc' %}<img src="/static/img/downarrow_pressed.gif" />{% endif %}
{% if request.GET.sort != 'price-desc' %}<img src="/static/img/downarrow.gif" />{% endif %}</a>
<a> <a href="?sort=price-asc">{% if request.GET.sort == 'price-asc' %}<img src="/static/img/uparrow_pressed.gif" /> {% endif %}
{% if request.GET.sort != 'price-asc' %}<img src="/static/img/uparrow.gif" />{% endif %}</a></li>
</ul>
</div>