代码之家  ›  专栏  ›  技术社区  ›  cer

django-tables2链接列链接指向错误项

  •  0
  • cer  · 技术社区  · 7 年前

    我有一个Django项目,我对指向错误条目的超链接(Django-Tables2中的LinkColumn)有问题,我无法弄清楚它发生的原因或如何修复它。

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

    我在整个项目中多次使用django-tables2,并且喜欢这些表的呈现方式,但是我不知道如何解决这个问题。我已经包括在内了 一些 我的代码(请注意,我包含了一些pi和示例模型,但不是全部)。

    非常感谢您的帮助。

    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 回复  |  直到 7 年前
        1
  •  2
  •   ruddra    7 年前

    嗯,通过 pk 通过访问器意味着,它将传递发布模型对象的主键 pi-details , sample-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')])
    
    推荐文章