Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-tables2 doesn't sort

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?

like image 908
giZm0 Avatar asked Jul 04 '12 14:07

giZm0


1 Answers

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})
like image 93
bruno desthuilliers Avatar answered Sep 25 '22 04:09

bruno desthuilliers