gpt4 book ai didi

python - django-tables2 不排序

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:22 24 4
gpt4 key购买 nike

无法对 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/p>

我做错了什么?

最佳答案

您需要一个 RequestConfig 对象,如 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})

关于python - django-tables2 不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com