django-tables2 linkColumn external url

2019-05-10 05:46发布

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

2条回答
Emotional °昔
2楼-- · 2019-05-10 06:04

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

查看更多
混吃等死
3楼-- · 2019-05-10 06:27

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.

查看更多
登录 后发表回答