django-tables2没有排序

giZ*_*Zm0 4 python python-2.7 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
Run Code Online (Sandbox Code Playgroud)

而mymodel.html看起来如下:

{% load render_table from django_tables2 %}
{% render_table table %}
Run Code Online (Sandbox Code Playgroud)

这会使表格正确,但在浏览器中单击列时没有任何反应.其他然后urld改变http://127.0.0.1:8000/show_mymodel- >http://127.0.0.1:8000/show_mymodel?sort=name

我做错了什么?

bru*_*ers 10

您需要一个RequestConfig对象,如教程中所述:

使用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})
Run Code Online (Sandbox Code Playgroud)