Question

can't get sorting working for a django-tables2 table.

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

And mymodel.html looks as follows:

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

This renders the table correct but nothing happens when clicking the columns in the browser. Other then the urld change http://127.0.0.1:8000/show_mymodel --> http://127.0.0.1:8000/show_mymodel?sort=name

What is it that I'm doing wrong?

Was it helpful?

Solution

You need a RequestConfig object as explained in the tutorial:

Using RequestConfig automatically pulls values from request.GET and updates the table accordingly. This enables data ordering and pagination.


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})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top