3

My app displays a table with many columns. I use Django tables 2 app to render the table. I am trying to make items in one column hyperlinked so that users can click. The url pattern is simple: /contact/pk/, for e.g. /contact/2/. This is what I have in my models:

#models.py

class Contact(models.Model):
    name = models.CharField(max_length=200)
    . . .

class ContactTable(tables.Table):

    name = tables.LinkColumn('contact_detail', args=[A('pk')])
    class Meta:
        model = Contact
        attrs = {"class": "paleblue"}

#urls.py

url(r'^contact/(?P<item_id>\d+)/$', 'app.views.contact_view', name='contact_detail'),

However, the items do not get hyperlinked.

David Maness
  • 91
  • 12
nixnotwin
  • 2,343
  • 8
  • 30
  • 46
  • I think this will help you: http://stackoverflow.com/questions/8613542/the-linkcolumn-about-django-tables2 – catherine Feb 09 '13 at 07:29

3 Answers3

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"}
nixnotwin
  • 2,343
  • 8
  • 30
  • 46
  • This adds the new column to the end of the table, is there anyway to determine the position of the column too? – Ibo Aug 30 '18 at 16:45
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>')
Joel Aufrecht
  • 442
  • 1
  • 5
  • 16
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.

Adam Taylor
  • 4,691
  • 1
  • 37
  • 38
  • My form works fine while using the basic setup provided at Django tables 2 docs page. I want to add one more feature where the items in one columns get hyperlinked. E.g.: a table with name and age: John and 35. I want to e able to click on John and get lead to a page where I can modify his age. I have the edit form, but it needs to be linked to the data in the rendered tables with the url pattern I have mentioned above. – nixnotwin Feb 09 '13 at 07:48
  • @nixnotwin What are you passing to `render_table` in your template? – Adam Taylor Feb 09 '13 at 07:56
  • Exactly what you have posted in your answer above. I have not made any changes to the default view mentioned in the docs. – nixnotwin Feb 09 '13 at 08:00
  • @nixnotwin Have you done a sanity check? For example, change the class from "paleblue" to something else and see if that works? – Adam Taylor Feb 09 '13 at 08:03
  • I solved the issue. I have posted the solution as an answer here. – nixnotwin Feb 09 '13 at 09:00
  • @nixnotwin I'm glad you got it resolved! I updated my answer with a piece of information that may or may not have also resolved your problem. I did it just in case somebody else stumbles upon my answer and finds my update useful. – Adam Taylor Feb 10 '13 at 03:17