django-tables2:LinkColumn:详细视图链接未呈现

django-tables2: LinkColumn: detail view links not rendering

我在生成详细视图链接时遇到问题。有人可以为我提供一些关于我哪里出错的见解。

models.py

class Company(models.Model):
    STATE_CHOICES = (
        ('nsw', 'NSW'),
        ('nt', 'NT'),
        ('qld', 'QLD'),
        ('vic', 'VIC'),
        ('wa', 'WA'),
        ('tas', 'TAS'),
        ('act', 'ACT'),
        ('sa', 'SA')
    )

    company_name = models.CharField(max_length = 100)
    client_code = models.CharField(max_length = 100)
    company_state = models.CharField(max_length = 3,choices = STATE_CHOICES,)

    def __str__(self):
        return self.company_name

    def get_absolute_url(self):
        return reverse('company_list')

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.CompanyList.as_view(), name='company_list'),
    path('<int:pk>/', views.CompanyDetailView.as_view(), name='company_detail'),
    path('new/', views.CompanyCreateView.as_view(), name='company_new'),
]

views.py

import django_tables2 as tables
from django_tables2 import SingleTableView
from django_tables2.utils import A 

class CompanyTable(tables.Table):
    class Meta:
        model = Company
        attrs = {'class': 'mytable table table-striped table-bordered table-hover'}
        company_name = tables.LinkColumn('company_detail', args=[A('pk')])
        orderable = False     

class CompanyList(SingleTableView):
    model = Company
    table_class = CompanyTable


class CompanyDetailView(DetailView):
    model = Company

我在这里错过了什么?我看过其他堆栈问题,但似乎无法弄清楚。

当您覆盖 Table class 的属性时,您需要将其作为 Table class 的属性,而不是 [=13] =] class。因此,您需要像这样定义 table:

class CompanyTable(tables.Table):
    company_name = tables.LinkColumn('company_detail', args=[A('pk')])  # not in meta
    class Meta:
        model = Company
        attrs = {'class': 'mytable table table-striped table-bordered table-hover'}
        orderable = False