首页 > 解决方案 > Django-Tables2 LinkColumn 链接指向错误的项目

问题描述

我有一个 Django 项目,我遇到了指向错误条目的超链接(来自 Django-tables2 的 LinkColumn)的问题,我无法弄清楚它为什么会发生或如何修复它。

非常具体地说,我可以转到管理视图并创建一个出版物。在设置作者(又名 pi)或样本时,有一个外键字段(样本/pi)的下拉菜单,其中显示了我可以从中选择的所有现有条目。当我选择一个样本和 pi 时,然后查看表格渲染,超链接就在那里,样本、pi 和出版物标题。出版物标题正确地将我带到了 publication_detail 页面。示例的超链接会将我带到示例详细信息页面,但它与我从管理页面中选择的示例不同。我对作者有同样的问题;它把我带到了一个作者的详细视图页面,而不是我从管理页面中选择的那个。

我在整个项目中多次使用 django-tables2 并且喜欢表格的呈现方式,但无法弄清楚如何解决这个问题。我已经包含了我的一些代码(请注意我包含了一些 PI 和 Sample 模型,但不是全部)。

非常感谢任何帮助。

models.py

class PI(models.Model): #this is a smattering of the PI model
   l_name = models.CharField('L Name', blank=False, max_length=100, default='')
   f_name = models.CharField('F Name', blank=False, max_length=100, default='')
   m_name = models.CharField('MI', null=True, blank=True, max_length=1, default='' )
   phone = PhoneField(blank=True, default='')
   email = models.EmailField('Email', blank=True, max_length=100, default='')

class Sample(models.Model): #this is a smattering of the Sample model
   sample_name = models.CharField('Sample', max_length=16)
   pi = models.ForeignKey(PI, on_delete=models.SET_NULL, null=True)
   submitter = models.ForeignKey('Submitter', blank=True, on_delete=models.SET_NULL, null=True)

class Publication(models.Model):
   sample = models.ForeignKey(Sample, on_delete=models.SET_NULL, null=True)
   author = models.ForeignKey(PI, on_delete=models.SET_NULL, null=True)
   title_p = models.CharField('Title', max_length=200, blank=False, default='')
   volume = models.IntegerField('Volume', blank=True, null=True)
   number = models.IntegerField('Number', blank=True, null=True)
   pages = models.CharField('Pages', default='', max_length=20, blank=True)
   year = models.IntegerField('Year', blank=True, null=True)
   doi = models.CharField('DOI', default='', max_length=30, blank=False)
   journal = models.CharField('Journal', default='', max_length=100, blank=False)
   abstract = models.CharField('Abstract', default='', max_length=1000, blank=False)
   issn = models.CharField('ISSN', default='', max_length=10, blank=False)
   url = models.CharField('URL', default='', max_length=100, blank=False)
   eprint = models.CharField('Eprint', default='', max_length=100, blank=False)

   class Meta:
      ordering = ('sample', 'author', 'title_p', 'journal', 'volume', 'number', 'pages', 'year', 'doi', 'abstract', 'issn', 'url', 'eprint')

   def get_absolute_url(self):
      return reverse('publication-detail', args=[str(self.id)])

   def __str__(self):
      return f'{self.sample}, {self.author}, {self.title_p}, {self.volume}, {self.number}, {self.pages}, {self.year}, {self.doi}, {self.journal}, {self.abstract}, {self.issn}, {self.url}, {self.eprint}'

tables.py

class PublicationTable(tables.Table):
   sample = tables.LinkColumn('sample-detail', args=[A('pk')])
   author = tables.LinkColumn('pi-detail', args=[A('pk')])
   title_p = tables.LinkColumn('publication-detail', args=[A('pk')])

   class Meta:
      model = Publication
      fields = ( 'sample', 'author', 'title_p', 'journal', 'year', )
      exclude = ( 'volume', 'number', 'pages', 'doi', 'abstract', 'issn', 'url', 'eprint', )
      list_display = ('sample', 'author', 'title_p', 'year', 'journal', )

views.py

class PublicationListView(generic.ListView):
   model = Publication
   paginate_by = 100

@login_required
def publication_view(request, pk):
   publication = Publication.objects.get(pk = pk)
   table = PublicationTable(Publication.objects.filter(publication=pk))
   RequestConfig(request).configure(table)
   return render(request, 'samples/publication_detail.html', {'publication': publication, 'publication-detail': table}) 

@login_required
def publication_table(request):
   table = PublicationTable(Publication.objects.all())
   RequestConfig(request).configure(table)
   return render(request, 'samples/publication_list.html', {'publication_table': table}) 

class PublicationDetailView(generic.DetailView):
    model = Publication

urls.py

urlpatterns = [ 
   path('', views.index, name='index'),
   path('samples/', views.sam, name='sam'),
   path('sample/<int:pk>', views.SampleDetailView.as_view(), name='sample-detail'),
   path('pi/', views.pi_table, name='pi_table'),
   path('pi/<int:pk>', views.pi_view, name='pi-detail'),
   path('publication/', views.publication_table, name='publication_table'),
   path('publication/<int:pk>', views.PublicationDetailView.as_view(), name='publication-detail'),
]

一些代码来自samples/templates/samples/publication_list.py

{% render_table publication_table %}

标签: djangodjango-tables2

解决方案


嗯,pk通过访问器意味着,它将传递发布模型对象的主键pi-detailssample-details等等。因此,您需要对其进行更改,以便将相应的主键传递给访问器,如下所示:

class PublicationTable(tables.Table):
   sample = tables.LinkColumn('sample-detail', args=[A('sample_id')])
   author = tables.LinkColumn('pi-detail', args=[A('author_id')])
   title_p = tables.LinkColumn('publication-detail', args=[A('pk')])

推荐阅读