Reputation: 79
I'm having difficulty in generating detail view links. Could somebody provide me with some insight as to where I'm going wrong.
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
What am I missing here? I've looked at other stack questions and I can't seem to figure it out.
Upvotes: 0
Views: 453
Reputation: 51988
When you are overriding an attribute of the Table
class, you need to put it as attribute of the Table
class, not in the Meta
class. So, you need to define the table like this:
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
Upvotes: 1