开发者

Transpose Django Table in django_tables2

开发者 https://www.devze.com 2023-03-24 14:16 出处:网络
I\'m trying to make a view that essentially shows information about a record like this Description| blahblahblah

I'm trying to make a view that essentially shows information about a record like this

|Description| blahblahblah
|Name       | blah blah blahs
|Last Edited| someDate
|Owned by   | Blah blah blah info

which is transposed from the way that django_tables2 renders tables by default. Is there an easy way to do this or will I have to code my own template? I'm find doing that (in theory) as there's a good example of a custom t开发者_如何学编程emplate which says I have to "pass an instance of your Table subclass into your own template, and render it yourself". I don't actually know what's meant by that though :(


You will need to write your own template that produces the HTML you desire, by iterating over attributes of the table and writing the correct tags. A good example of how you might do this is the template used for the as_html() function: https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html


I solved this for a project today, it was actually fairly simple.

Define a new table

Within tables.py, define a two-column table. Let's call the first column name and second column value:

class RowTable(tables.Table):
    name = tables.Column()
    value = tables.Column()

    class Meta:
        template = 'django_tables2/bootstrap.html'
        attrs = {'class': 'table table-striped table-responsive'}  

In your view, bind data to table

Then, in your view, you need to pull the instance (row) from your database, and populate the table using a dictionary:

row_instance = Model.objects.get(foo=foo.id)
#Extract column data from instance be sure it is string!
height = str(row_instance.height)
weight = str(row_instance.weight)
nationality = row_instance.nationality

#Create dictionary of data in form table will recognize
row_data =[ {'name': 'height', 'value': height},
            {'name': 'weight', 'value': weight},
            {'name': 'nationality', 'value': nationality} ]

#Bind to table and configure
table = RowTable(row_data)

Then configure the table, put in context and send to template as usual. You will have a table with two columns showing all the data from a row of your database.


Change the result_list(cl) function in /lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py to this:

def result_list(cl):
 """
 Displays the headers and data list together
 """
 headers = list(result_headers(cl))
 num_sorted_fields = 0
 for h in headers:
     if h['sortable'] and h['sorted']:
         num_sorted_fields += 1
 result1 = list(results(cl))
 result2 = map(list, zip(*result1))
 return {'cl': cl,
         'result_hidden_fields': list(result_hidden_fields(cl)),
         'result_headers': headers,
         'num_sorted_fields': num_sorted_fields,
         'results': result2}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号