我在生成详细信息视图链接时遇到了困难。有人可以向我提供一些关于我哪里出错的见解。模型.pyclass 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')网址.pyfrom django.urls import pathfrom . import viewsurlpatterns = [ 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'),]视图.pyimport django_tables2 as tablesfrom django_tables2 import SingleTableViewfrom 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 = CompanyTableclass CompanyDetailView(DetailView): model = Company我在这里错过了什么?我看过其他堆栈问题,但似乎无法弄清楚。
1 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
当您覆盖Table类的属性时,您需要将其作为Table类的属性,而不是在Meta类中。因此,您需要像这样定义表:
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
添加回答
举报
0/150
提交
取消