Use self.kwargs.get(‘gwpk’)
https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-display/#dynamic-filtering


From: [email protected] [mailto:[email protected]] On 
Behalf Of RisenFenix
Sent: Friday, March 16, 2018 7:55 AM
To: Django users
Subject: Question about Getting URL segment into a View

I am a newby to Django (2.0) and need some assistance, please.  I have a list 
of Grocery Wholesale Vendors in one model and their Contacts in another with 
one to many join.

The Model...

from django.conf import settings
from django.db import models
from django.urls import reverse

[...]

#Grocery Wholesale Table
class GroceryWholesale(models.Model):
    grocery_wholesaler = models.CharField(max_length=150)
    grocery_wholesaler_active = models.BooleanField(default=True)

    def __str__(self):
        """A string representation of the model."""
        return self.grocery_wholesaler[:50]

    class Meta:
        verbose_name_plural = "Grocery Wholesalers"

#Grocery Wholesale Contacts Table
class GroceryWholesaleContacts(models.Model):
    grocery_wholesaler = models.ForeignKey(GroceryWholesale, 
on_delete=models.CASCADE)
    contact_name = models.CharField(max_length=150, null=True)
    contact_phone = models.CharField(max_length=75,null=True)
    contact_mobile = models.CharField(max_length=75,null=True)
    contact_fax = models.CharField(max_length=75, null=True)
    contact_email = models.CharField(max_length=150, null=True)
    contact_notes = models.CharField(max_length=200, null=True)
    contact_active = models.BooleanField(default=True)

    def __str__(self):
        """A string representation of the model."""
        return self.contact_name[:50]

    class Meta:
        verbose_name_plural = "Grocery Wholesaler Contacts"

Then in my URLs I have

from django.urls import path
from . import views

urlpatterns = [
    path('gwlist', views.GroceryWholesalersListView.as_view(), 
name='grocerywholesalers'),
    path('gwclist/<int:gwpk>/', 
views.GroceryWholesalersContactsListView.as_view(), 
name='grocerywholesalerscontacts'),
]

My Views I have

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView, DetailView
from django.views.generic import TemplateView
from django.urls import reverse
from django.http import HttpResponse
from . import models
from .models import GroceryWholesale, GroceryWholesaleContacts

# Create your views here.
class GroceryWholesalersListView(ListView):
    model = models.GroceryWholesale
    context_object_name = 'grocery_wholesalers'
    template_name = 'grocery_wholesaler_list.html'
    ordering = ['grocery_wholesaler']

class GroceryWholesalersContactsListView(ListView):
    model = models.GroceryWholesaleContacts
    context_object_name = 'grocery_contacts'
    template_name = 'grocery_wholesaler_contacts_list.html'
    ordering = ['contact_name']
    def get_queryset(self):
        return 
GroceryWholesaleContacts.objects.filter(grocery_wholesaler_id=request.gwpk)

It's the part in Red I am having trouble with.  If I hand type a primary key 
for the Grocery Wholesaler I get the correct filtered list of contacts
IE
return GroceryWholesaleContacts.objects.filter(grocery_wholesaler_id='90') and 
that pulls up the Grocery Wholesaler contacts filtered to ONLY show the ones 
belonging to grocery_wholesale_id #90

BUT I am trying to figure out how to pull the PK code dynamically from the URL 
instead and I can't figure it out or what it is called to google for help.  My 
URL looks like this...  http://127.0.0.1:8000/clients/gwclist/90/

Any help is greatly appreciated.  I hope I explained this correctly.
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3a144ad0-ee41-406e-8eb6-532c001ac152%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3a144ad0-ee41-406e-8eb6-532c001ac152%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/94862a184dfe4b14bd71f29a933bf95d%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to