Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-tables2 with custom image cell

in a simple django-tables2 how can i render an imagen in specific cell for 1 colum....

some like

   field1     field2   field3    .   .   .

row1 A image1 C

row2 B image2 D

. . .

like image 347
emper0r Avatar asked Jan 01 '13 03:01

emper0r


1 Answers

At Django Tables2 documentation:

http://django-tables2.readthedocs.org/en/latest/#subclassing-column

Example of this section is ImageColumn.

For complicated columns, you may want to return HTML from the render() method. This is fine, but be sure to mark the string as safe to avoid it being escaped:

>>> from django.utils.safestring import mark_safe
>>> from django.utils.html import escape
>>>
>>> class ImageColumn(tables.Column):
...     def render(self, value):
...         return mark_safe('<img src="/media/img/%s.jpg" />'
...                          % escape(value))
...

Link to official documentation: https://github.com/bradleyayers/django-tables2/blob/master/docs/index.rst (just in case)

like image 173
n3storm Avatar answered Nov 15 '22 06:11

n3storm