4

I have 2 model attributes - model.name and model.url I need to create a linkColumn that column name = model.name and link to the url specified in model.url

Is it possible to achieve such thing?

thanks

DjangoPy
  • 855
  • 1
  • 13
  • 29

2 Answers2

11

You can use TemplateColumn to achieve it. Your tables.py should look something like this

# yourapp/tables.py
import django_tables2 as tables
from yourapp.models import yourmodel
class YourTable(tables.Table):
    name = tables.TemplateColumn('<a href="{{record.url}}">{{record.name}}</a>')
    class Meta:
        model = yourmodel
        fields = ('name') # fields to display

You may refer to the DOC, for more info.

Jose Cherian
  • 7,207
  • 3
  • 36
  • 39
  • 1
    Been looking on how to do this for a long time! The documentation is not as clear as your example, thank you! – Avagut Oct 20 '15 at 20:49
0

I achieved it by creating a custom column that queries the database and renders the link from the given attributes

DjangoPy
  • 855
  • 1
  • 13
  • 29