[Fixed]-Django-tables2 add button per row

16👍

You can create in your table a template column, this will render something, a button for example:

class MyTables(tables.Table):
  ...
  my_column = tables.TemplateColumn(verbose_name=_('My Column'),
                                    template_name='app/my_column.html',
                                    orderable=False) # orderable not sortable

In the template my_column the row is in the variable record:

{{ record.my_field }}
👤Goin

2👍

Example:
actions = TemplateColumn(template_code='<a href="{% url "review_detail" record.id %}" class="btn btn-success">View’) this line of code adds a column to your table and adds the html content you want. To pass the id to your route, just call the "record" object

import django_tables2 as tables
from django_tables2 import TemplateColumn
from .models import Review, ReviewCategory, ReviewLog, ItemCategory


class ReviewTable(tables.Table):
    acciones = TemplateColumn(template_code='<a href="{% url "review_detail" record.id %}" class="btn btn-success">Ver</a>')
    class Meta:
        model = Review
        exclude = (
            'id',
            'assigned',
            'parent',
            'created_at',
            'updated_at'
            )

Leave a comment