2012-04-14 38 views
1

這是代碼清單任務和排序它們的三個條件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> 

回答

0

關於第二個問題。如果在TEMPLATE_CONTEXT_PROCESSORS中有django.core.context_processors.request

檢查{{request.GET}}是否返回某些內容。

關於第一。也許在這裏的問題:

kwargs['queryset'] = q.all() 

試試這個:

kwargs['queryset'] = q 

而且你需要跟蹤什麼●在你看來

+0

是的,這是問題! context_processors工作。第一個問題仍然沒有解決。 – magu2 2012-04-18 19:04:42

0
  1. 檢查你的模板文件findtask.html的開頭和結尾,尤其是爲頁面生成URL鏈接的部分。我懷疑鏈接錯誤地錯過了sort參數。
  2. 在設置文件中檢查TEMPLATE_CONTEXT_PROCESSORS,在那裏是the request processor?在模板中訪問不存在的變量現在不會在Django中引發錯誤。
+0

謝謝!這是context_processors。 – magu2 2012-04-18 19:05:03