2012-07-04 42 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