[Django]-Django tables 2: Hyperlinking items in a column

13👍

This solved it:

class ContactTable(tables.Table):
    edit_entries = tables.TemplateColumn('<a href="/contact/{{record.id}}">Edit</a>')

    class Meta:
        model = Contact
        attrs = {"class": "paleblue"}

5👍

nixnotwin’s solution uses hard-coded URLs. To use reverse lookup urls:

class ContactTable(tables.Table):
    edit_entries = tables.TemplateColumn('<a href="{% url \'contact_detail\' record.id %}">Edit</a>')

0👍

What are you passing to render_table in your template? Just a regular QuerySet? My guess is you forgot to instantiate and configure the table in your view. Here is the example provided in the docs:

# tutorial/views.py
from django.shortcuts import render
from django_tables2   import RequestConfig
from tutorial.models  import Person
from tutorial.tables  import PersonTable

def people(request):
    table = PersonTable(Person.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'people.html', {'table': table})

If you do it like this, it should work fine.

UPDATE:

I know that the problem has already been resolved, but I noticed that the name = tables.LinkColumn('contact_detail', args=[A('pk')]) line of code is within the ContactTable class’s inner Meta class. It should be outside of the inner Meta class.

Leave a comment