Reputation: 849
Views.py
class citydetailview(generic.DetailView):
#Generic class-based list view for a list of cities.
model = City
def get_city_value(request, pk):
if pk==1:
hyd=Type_city1.objects.all()
elif pk==2:
hyd=Type_city2.objects.all()
elif pk==3:
hyd=Type_city3.objects.all()
return (request,{'hyd':hyd})
urls.py
path('city/<int:pk>', views.citydetailview.as_view(), name='city_ads_detail'),
I want to use the 'pk' value from the urls.py in one of my class and render the output accordingly to template
Upvotes: 0
Views: 58
Reputation: 51978
Rather than, why not use a List View:
class CityDetailView(generic.ListView):
model = City
def get_queryset(self):
city_type = self.kwargs['city_type']
if city_type == 1:
return Type_city1.objects.all()
elif city_type == 2:
return Type_city2.objects.all()
elif city_type == 3:
return Type_city3.objects.all()
return super().get_queryset()
# urls
path('city/<int:city_type>/', views.CityDetailView.as_view(), name='city_ads_detail'),
Finally, probably its better to share your models as well. Because I think your model structure probably isn't right. Rather than having different Type_city
models, you could have stored them in a single model and filter based on a field ie city_type
.
Upvotes: 1