2012-07-04 39 views
3

無法獲得對django-tables2表的排序工作。django-tables2不排序

class MyModel(models.Model): 
    pid = models.AutoField('id',primary_key = True) 
    name = models.CharField(max_length = 255, 
          help_text='The name') 
def show_mymodels(request): 
    """ the view """ 
    table = MyModelTable(MyModel.objects.all()) 
    return render(request,'mymodel.html',{'table':table}) 

class MyModelTable(tables.Table): 
    class Meta: 
     model = MyModel 
     orderable = True 

而且mymodel.html如下所示:

{% load render_table from django_tables2 %} 
{% render_table table %} 

這使得該表正確的,但在點擊瀏覽器中的列時沒有任何反應。其他然後urld變化http://127.0.0.1:8000/show_mymodel - >http://127.0.0.1:8000/show_mymodel?sort=name

這是什麼,我做錯了?

回答

7

你需要一個RequestConfig對象作爲tutorial解釋說:

使用RequestConfig自動request.GET拉值,並相應地更新表。這使數據排序和分頁。


from django_tables2 import RequestConfig 

def show_mymodels(request): 
    table = MyModelTable(MyModel.objects.all()) 
    RequestConfig(request).configure(table) 
    return render(request, 'mymodel.html', {'table': table}) 
+0

輝煌!我已經翻閱了幾次文檔,但只是在訂購時。我已經在提到它的地方使用了Ordering-link。我無法理解我是如何錯過它的。 – giZm0

+0

@ giZm0:有趣的部分是我迄今爲止從未使用django-tables(但我肯定會在我們的新大項目中使用它),只是使用了它,答案就在這裏的第一頁FineManual 。 –

+0

@bruna desthuilliers:好吧,如果我至少可以傷心的是咖啡機壞了,但我沒有任何藉口。關於表格2;我不能說我已經使用了很多,這很明顯,但是到目前爲止我看到的是它是一個非常好的工具! – giZm0