django-tables2 linkColumn external url
Asked Answered
S

2

5

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

Silverplate answered 3/11, 2012 at 16:51 Comment(4)
It's not clear: would you like to make a request or make some changes in your existing models?Barre
I achieved it by creating a custom column that renders the link.Silverplate
Found this question while looking for something similar. Added my solution as an answer.Brendanbrenden
I know this might be an old posting, but you can handle that by creating a view and redirect user to the external link. simple as thatShih
B
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.

Brendanbrenden answered 14/11, 2012 at 7:2 Comment(1)
Been looking on how to do this for a long time! The documentation is not as clear as your example, thank you!Drillstock
S
0

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

Silverplate answered 3/11, 2012 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.