How to render a table from custom sql using django-tables2?

I work with Django and django-tables2 to make a good presentation of sql queries in the web interface. I have an old sql code that is very very complex to define it through standard .py models.

The question is: how can I visualize a table from a custom SQL query using django-tables2?

+4
source share
2 answers

Documents populate the table with data showing how you can create a table with a list of dictionaries as input.

import django_tables2 as tables data = [ {"name": "Bradley"}, {"name": "Stevie"}, ] class NameTable(tables.Table): name = tables.Column() table = NameTable(data) 

Assuming your custom SQL query returns data in a similar format, you should use the same approach.

+2
source

Well, I did not understand the concept of djt2 correctly. Therefore, I had to do this with a conditional statement. And now it works fine with django Manager.raw () and returns nice tables.

 {% load render_table from django_tables2%} <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" /> {% if result%} {%render_table result%} {%endif%} 

`

0
source

All Articles