Django-Tables2 LinkColumn link 转到错误的项目
Django-Tables2 LinkColumn link goes to wrong item
我有一个 Django 项目,超链接(来自 Django-tables2 的 LinkColumn)出现错误条目,我无法弄清楚它发生的原因或如何修复它。
非常具体,我可以转到管理视图并创建一个发布。在设置作者 (a.k.a.pi) 或样本时,有一个外键字段 (sample/pi) 的下拉菜单,显示所有现有条目,我可以从中选择一个。当我选择一个样本和 pi 然后查看 table 渲染时,样本、pi 和出版物标题的超链接就在那里。出版物标题正确地将我带到 publication_detail 页面。该示例的超链接会将我带到示例详细信息页面,但它与我从管理页面选择的示例不同。我对作者有同样的问题;它会将我带到一位作者的详细视图页面,而不是我从管理页面选择的页面。
我在整个项目中多次使用 django-tables2 并且喜欢 tables 的呈现方式,但无法弄清楚如何解决这个问题。我包含了 一些 我的代码(请注意我包含了一些 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 %}
嗯,通过访问器传递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')])
我有一个 Django 项目,超链接(来自 Django-tables2 的 LinkColumn)出现错误条目,我无法弄清楚它发生的原因或如何修复它。
非常具体,我可以转到管理视图并创建一个发布。在设置作者 (a.k.a.pi) 或样本时,有一个外键字段 (sample/pi) 的下拉菜单,显示所有现有条目,我可以从中选择一个。当我选择一个样本和 pi 然后查看 table 渲染时,样本、pi 和出版物标题的超链接就在那里。出版物标题正确地将我带到 publication_detail 页面。该示例的超链接会将我带到示例详细信息页面,但它与我从管理页面选择的示例不同。我对作者有同样的问题;它会将我带到一位作者的详细视图页面,而不是我从管理页面选择的页面。
我在整个项目中多次使用 django-tables2 并且喜欢 tables 的呈现方式,但无法弄清楚如何解决这个问题。我包含了 一些 我的代码(请注意我包含了一些 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 %}
嗯,通过访问器传递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')])