这是代码列出任务并按desc和asc三个条件对它们进行排序。
我现在有两个问题,那就是:
1)order_by仅应用于第一页。我希望它能运行order_by,然后对整个有序列表进行分页
2)从未显示“_压缩”箭头图像。
请帮忙!谢谢!
VIEW.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)
URL.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>
答案 0 :(得分:0)
关于第二个问题。如果TEMPLATE_CONTEXT_PROCESSORS中有django.core.context_processors.request?
检查{{request.GET}}是否返回了某些内容。
关于第一个。也许这里有问题:
kwargs['queryset'] = q.all()
试试这个:
kwargs['queryset'] = q
你需要在视图的开头和结尾跟踪q中的内容
答案 1 :(得分:0)
findtask.html
,尤其是生成网页链接的部分。我怀疑链接错误地错过了sort
参数。