Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-tables2 linkColumn external url

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

like image 912
DjangoPy Avatar asked Jan 16 '23 03:01

DjangoPy


1 Answers

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.

like image 77
Jose Cherian Avatar answered Jan 17 '23 16:01

Jose Cherian