Я сделал вилку из django-tables
, что делает это чрезвычайно простым. Вот простой пример:
В models.py
:
from django.db import models
class City(models.Model):
name = models.CharField(max_length=200)
state = models.CharField(max_length=200)
country = models.CharField(max_length=200)
In tables.py
:
import django_tables as tables
from .models import City
class CityTable(tables.Table):
class Meta:
model = City
В views.py
:
from django.shortcuts import render_to_response
from django.template import RequestContext
from .models import City
from .tables import CityTable
def city_list(request):
queryset = City.objects.all()
table = CityTable(queryset)
return render_to_response("city_list.html", {"city_table": table},
context_instance=RequestContext(request))
В city_list.html
:
{% load django_tables %}
{% render_table city_table %}