Django Tables2 with TemplateColumn the View is not printing the request

58 views Asked by At

I'm trying to print the request in my view but nothing happens when I click on the 'Editar' button (TemplateColumn), this is what I have in my code:

tables.py

import django_tables2 as tables
from django_tables2 import TemplateColumn
from .models import Vencimientos, LogAsistencia

class VencimientosTable(tables.Table):

    asistencia = TemplateColumn(
            '<a class="btn btn btn-info btn-sm" href="{% url "checkin" record.id %}">Editar</a>')
    class Meta:
        model = Vencimientos
        template_name = "django_tables2/bootstrap5.html"
        fields = ("cliente","vencimiento","activo" )
        attrs = {"class": "table table-hover table-sm"}

urls.py

urlpatterns = [
    
    ....
    path('asistencia/<int:pk>/', CheckIn.as_view(), name='checkin')

] 

views.py

class CheckIn(View):

    def get(self, request):
        print(request)
        return redirect ('asistencia')

when I click on the "Editar" button in the table the idea is to get the record.id so I can add some extra code, but the button doesn´t do anything

UPDATE

I inspect the button and I see the link correct:

enter image description here

The button doesn´t do anything still

1

There are 1 answers

1
Ben On

Use a custom render_ method to achieve what you're trying to do.

from django.urls import reverse
from django.utils.html import format_html

class VencimientosTable(tables.Table):

    asistencia = tables.Column(empty_values=())

    def render_asistencia(self, record):
        url = reverse("checkin", kwargs={"pk": record.id})
        return format_html(f"<a class='btn btn btn-info btn-sm' href='{url}'>Editar</a>")