# tutorial/models.py class Person(models.Model): name = models.CharField('full name', max_length=50)
# tutorial/views.py from django.shortcuts import render def people(request): return render(request, 'people.html', {'people': Person.objects.all()})
{# tutorial/templates/people.html #} {% load render_table from django_tables2 %} {% load static %} <!doctype html> <html> <head> <link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" /> </head> <body> {% render_table people %} </body> </html>
# tutorial/tables.py import django_tables2 as tables from .models import Person class PersonTable(tables.Table): class Meta: model = Person # add class="paleblue" to <table> tag attrs = {'class': 'paleblue'}
# tutorial/views.py from django.shortcuts import render from django_tables2 import RequestConfig from .models import Person from .tables import PersonTable def people(request): table = PersonTable(Person.objects.all()) RequestConfig(request).configure(table) return render(request, 'people.html', {'table': table})
{% render_table table %}
Source: https://habr.com/ru/post/326194/
All Articles