猿问

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

我在生成详细信息视图链接时遇到了困难。有人可以向我提供一些关于我哪里出错的见解。


模型.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')

网址.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'),

]

视图.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

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


汪汪一只猫
浏览 159回答 1
1回答

幕布斯7119047

当您覆盖Table类的属性时,您需要将其作为Table类的属性,而不是在Meta类中。因此,您需要像这样定义表:class CompanyTable(tables.Table):&nbsp; &nbsp; company_name = tables.LinkColumn('company_detail', args=[A('pk')])&nbsp; # not in meta&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; model = Company&nbsp; &nbsp; &nbsp; &nbsp; attrs = {'class': 'mytable table table-striped table-bordered table-hover'}&nbsp; &nbsp; &nbsp; &nbsp; orderable = False&nbsp;&nbsp;
随时随地看视频慕课网APP

相关分类

Python
我要回答