Django-Tables2 LinkColumn链接转到错误的项目

时间:2018-12-31 15:18:24

标签: django django-tables2

我有一个Django项目,但超链接(Django-tables2中的LinkColumn)的输入有问题,我无法弄清它为什么发生或如何解决。

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

我在整个项目中多次使用django-tables2,并且喜欢表的呈现方式,但是无法弄清楚如何解决该问题。我在代码中包含了 some (请注意,我包含了一些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 %}

1 个答案:

答案 0 :(得分:2)

好吧,通过访问器方式传递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')])