如何获取linkify以将您带到Django表的详细视图?

时间:2019-02-12 05:00:25

标签: django django-views django-tables2

我正在尝试创建从django-tables2列到对象的“详细视图”的链接。例如,我有一列健康提供者,我希望用户能够单击它,并从django搜索结果表中查看健康提供者的特定信息。

当前,它不起作用。我可以显示该表,但是无法将其单击到详细信息页面的位置。请帮忙。我的网址和视图有问题,因此我将包括tables.py,views.py和urls.py。我要链接的链接是价格模型中医院模型的外键。

这是urls.py。这是在url_patterns下。

  path('hospital_detail/(?P<pk>\d+)/$', views.HospitalDetailView.as_view(), name='hospital-detail')

这是views.py。

from django.template import RequestContext, get_object_or_404, render
from django.views.generic.detail import DetailView
from catalog.models import Price, Hospital, Service
from django_tables2 import RequestConfig
from catalog.tables import PriceTable

class HospitalDetailView(generic.DetailView):
    model = Hospital

def hospital_detail_view(request, primary_key):
    hop = get_object_or_404(Hospital,  pk=primary_key)
      return render(request, 'hospital_detail.html', {'hospital': hop})

def results(request):
    if request.method == "GET":
      Q=()
      out_words = request.GET.get('s')
      context = RequestContext(request)
      table = PriceTable(Price.objects.filter(service__desc_us__icontains = out_words))
      RequestConfig(request).configure(table)
    else:
      table = PriceTable(Price.objects.all())
      RequestConfig(request).configure(table)

    return render(request, 'results.html', {'table': table})

这是tables.py。医院是价格表中的外键。

import django_tables2 as tables
from catalog.models import Service, Hospital, Price
from django_tables2.utils import A

class PriceTable(tables.Table):
    hospital = tables.LinkColumn('hospital-detail', args=[A('pk')])

    class Meta:
        model = Price
        template_name = 'django_tables2/bootstrap.html'
        sequence = ('hospital', 'service', 'price_offer', 'pub_date')
        exclude = ('id', 'published')
        attrs = {"class": "paleblue"}

1 个答案:

答案 0 :(得分:-1)

使用tables.LinkColumn

tables.LinkColumn("hospital_detail", kwargs={"pk": tables.A("pk")}, empty_values=())