简体   繁体   中英

Django passing a foreign key from ListView to a Detailview

There is something I am not quite getting about views in Django. My site will have a table with different jobs or loads on the table. This is called a load board. This references a database table called Loadboard_table. The Loadboard_table contains a foreign key to the Company_table in the Database. This database table contains which Company assigned this job/load and other info on the company.

The objective is for the user to click on any row on the loadboard and this would bring the user to the Company's "Detail" page. I have shown a condensed part of the code below as to not bloat the question.

table row item on Index

<!-- 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>

Your help is greatly appreciated!

I found a way to reach the objective without using the DetailView Class. I simply made a function that processes the HTTP request and takes in the CompanyId from the url. I think the DetailsView class is used for a more specific situation.

as the user clicks on:

<td>
<a href="{% url 'loadboard:detail' item.CompanyName_id %}">{{item.CompanyName}}</a>
</td>

Then it triggers this URL pattern on urls.py:

url(r'^([0-9]+)/$', views.ViewCompanyDetails, name='detail')

Using the function on views.py:

def ViewCompanyDetails(request, CompanyId):
    CompanyObject = Company_table.objects.get(id = CompanyId)
    context = {'Company': CompanyObject}
    return render(request, 'loadboard/detail.html', context)

This achieves my objective of trying to pass the company id from where the user clicked on the table and pass it down all the way to the views page and get that HTML page.

I might be wrong about the DetailsView class from the generic model and it being too specific to primary keys, but someone can correct me on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM