0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer, i have added my models.py for your reference. Thanks Jai

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.