【发布时间】:2017-07-27 03:53:58
【问题描述】:
关于 Django 中的视图,我不太了解。我的站点将有一张桌子,桌子上有不同的工作或负载。这称为负载板。这引用了一个名为 Loadboard_table 的数据库表。 Loadboard_table 包含数据库中 Company_table 的外键。此数据库表包含分配此工作/负载的公司以及有关公司的其他信息。
目标是让用户点击加载板上的任何一行,这会将用户带到公司的“详细信息”页面。我已经在下面显示了代码的精简部分,以免使问题变得臃肿。
索引上的表格行项目
<!-- this is where I grab my Company table foreign key by clicking on one of the loadboard items -->
<td>
<a href="{% url 'loadboard:detail' item.CompanyName_id %}"> {{item.CompanyName}}</a>
</td>
urls.py
urlpatterns = [
# /loadboard/
url(r'^$', views.IndexView.as_view(), name='index'),
# /loadboard/71/
#note this is where I am trying to pass the foreign key to
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
views.py
from django.views import generic
from .models import Company_table, Loadboard_table
class IndexView(generic.ListView):
template_name = 'loadboard/index.html'
context_object_name = 'all_loadboard'
def get_queryset(self):
return Loadboard_table.objects.all()
class DetailView(generic.DetailView):
model = Company_table
template_name = 'loadboard/detail.html'
detail.html
<!-- I want this detail page to show the Company that the loadboard's foreign key references -->
<head>
<title>{% block title %}{{Company_table.CompanyName}}'s loadboard{% endblock %}</title>
</head>
非常感谢您的帮助!
【问题讨论】:
标签: python django-models sqlite django-templates django-views