django-tables2 linkColumn外部url地址

问题描述 投票:3回答:2

我有2个模型属性--model.name和model.url我需要创建一个linkColumn,列名=model.name并链接到model.url中指定的url。

能否实现这样的事情?

谢谢

django django-tables2
2个回答
7
投票

你可以使用TemplateColumn来实现它。你的tables.py应该是这样的

# 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

您可以参考 DOC,了解更多信息。


0
投票

我是通过创建一个自定义列来实现的,这个列可以查询数据库,并从给定的属性中显示链接。

© www.soinside.com 2019 - 2024. All rights reserved.