Reputation: 143
I have a problem with ListView and can't get queryset from the genre movies. If I go to categories I can see the movie category, but if I want to see movies by genre I see an empty list.
views.py
class MovieListView(ListView):
context_object_name = 'movies'
def get_queryset(self):
if self.kwargs.get('category') is not None:
movie_list = Movie.objects.filter(
category__slug=self.kwargs.get('category'),
published=True,
)
movies = movie_list
elif self.kwargs.get('genre') is not None:
movie_list = Movie.objects.filter(
genre__slug=self.kwargs.get('genre'),
published=True,
)
movies = movie_list
else:
movies = Movie.objects.filter(published=True)
return movies
urls.py
app_name = "movies"
urlpatterns = [
path('', MovieListView.as_view(), name='movie_list'),
path('<id>-<name>/', MovieDetailView.as_view(), name='movie_detail'),
path("<slug:category>/", MovieListView.as_view(), name="category_post"),
path("<slug:genre>/", MovieListView.as_view(), name="genre_list"),
]
Upvotes: 1
Views: 64
Reputation: 476699
The urls.py
does not make much sense. The patterns that match the '<slug:category>/'
patterns are exactly the same patterns as the ones that match '<slug:genre>/'
. So it will always takes the first path here.
You thus should make the paths distinct, for example by prefixing:
urlpatterns = [
path('', MovieListView.as_view(), name='movie_list'),
path('<id>-<name>/', MovieDetailView.as_view(), name='movie_detail'),
path('category/<slug:category>/', MovieListView.as_view(), name='category_post'),
path('genre/<slug:genre>/', MovieListView.as_view(), name='genre_list'),
]
Now the prefix will make it clear if you are filtering on category
or on genre
. You can of course make the distiction in a different way. But the most important is that the patterns should not overlap.
Upvotes: 1