Rendering Tables


To create a table from a model django-table2 can be used.

Add django-table2 to the requirements and config file under installed apps: ::

nano requirement/project.txt
...
-e git://github.com/bradleyayers/django-tables2.git#egg=django-tables2
...

nano settings.py
...
INSTALLED_APPS = (
    ...
    'django-tables2'
    ...

In the view pass the data, e.g.: ::

nano views.py
...
from django.shortcuts import render

from data.models import entry


def view(request):
    return render(request, 'entries.html', {'entries': Entry.objects.all()})

In the template load django-tables2, its static files and render the tables: ::

nano entries.html
{# Exemplifies rendering of a table #}
<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
    </head>
    <body>
         {% render_table entries %}
    </body>

If the static files are not rendered proably on deployment or need to be customized, copy the files into the respective media project directory: ::

cp -rf /home/user/project/env/src/django-tables2/django_tables2/static  /home/user/project/media/django_tables2/


Edit post (Admin)

Comment on This Data Unit