Listing reviews
Now, we want to list a movie's reviews on the movie details page (Figure 11.3):
Let's look at the steps to do so:
- In
/movie/views.py
, indef detail
, add the following code in bold:… def detail(request, movie_id): movie = get_object_or_404(Movie,pk=movie_id) reviews = Review.objects.filter(movie = movie) return render(request, 'detail.html', {'movie':movie, 'reviews': reviews}) …
Let's see what's happening in the code. Using the filter
function, we retrieve reviews for a particular movie only:
reviews = Review.objects.filter(movie = movie)
We then pass in the reviews to detail.html
:
...