Search code examples
pythondjangodjango-tables2

I want to represent a django table header as a checkbox


I'm making a django table using django-tables2. What I want is to create a header cell as a checkbox but the problem is django-tables2 doesn't seem to render html when passed as verbose. Here is my table code :

class AllTable(tables.Table):
   CHECKBOX_TEMPLATE = ' <input id="forms_check" type="checkbox" name="forms_check" pk="{{record.pk}}" /> '

   _ = tables.TemplateColumn(CHECKBOX_TEMPLATE)
   _.verbose_name = "<input type='checkbox'>"

   class Meta:
       model = models.All
       template_name = "django_tables2/bootstrap.html"
       fields = ("_","id","name")

The result i'm getting is as follow :

enter image description here

What I want is a checkbox instead. Please if anyone knows the way can you guide me through this. Thanks.


Solution

  • Simply use the mark_safe() method and you'll be able to use html.

    _.verbose_name = mark_safe("<input type='checkbox'>")
    

    https://docs.djangoproject.com/en/3.2/ref/utils/#django.utils.safestring.mark_safe

    Bonus: here's the code where that happens https://github.com/jieter/django-tables2/blob/e09632ee1cecdc27e826374212594e782bd22fbd/django_tables2/columns/base.py#L690