這是代碼清單任務和排序它們的三個條件desc和asc。
我現在有兩個問題,即:
1)order_by只應用於第一頁。我希望它運行order_by,然後將整個有序列表分頁。
2)「按下」箭頭圖像從不顯示。
請幫忙!謝謝!DJANGO order_by分頁之前
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>
是的,這是問題! context_processors工作。第一個問題仍然沒有解決。 – magu2 2012-04-18 19:04:42