Django - Filtering inside of get_context_data
<p>Using class-based views in Django, I'm having a problem filering inside of a DetailView.</p>

<p>What i would like to get is a list of all movies in a specific genre ie: <code>Movie.objects.all().filter(genre=genre_id)</code>.</p>

<pre><code>class GenreView(generic.DetailView):
    model = Genre
    template_name = 'movies/genre.html'
    context_object_name = 'this_genre'
def get_context_data(self, **kwargs):
    context = super(GenreView, self).get_context_data(**kwargs)
    context.update({
        'all_movies': Movie.objects.all().filter(genre=pk),
        'all_genres': Genre.objects.all()
    })
    return context
</code></pre>

<p>I get this error:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\detail.py", line 118, in get
    context = self.get_context_data(object=self.object)
  File "C:\Users\admin\trailers\movies\views.py", line 43, in get_context_data
    'all_movies': Movie.objects.all().filter(genre=pk),
NameError: name 'pk' is not defined
</code></pre>

<p>When I tried replacing <code>Movie.objects.all().filter(genre=pk)</code> with <code>Movie.objects.all().filter(genre=kwargs['pk'])</code> I got:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\detail.py", line 118, in get
    context = self.get_context_data(object=self.object)
  File "C:\Users\admin\trailers\movies\views.py", line 43, in get_context_data
    'all_movies': Movie.objects.all().filter(genre=kwargs['pk']),
KeyError: 'pk'
</code></pre>

<p>I can't seem to figure out what's wrong, any help would be appreciated!</p>

<p>The <code>kwargs</code> parameter passed to <code>get_context_data</code> doesn't contain the primary key value of the object. You can get it from <code>self.kwargs</code> though:</p>

<pre><code>Movie.objects.all().filter(genre=self.kwargs['pk'])
</code></pre>

<p>Furthermore, you will see <code>self.object</code> <em>(which is the the <code>Genre</code> instance fetched)</em> is present and accessible when you are inside <code>get_context_data</code>, so you can use that in order to do the following as well:</p>

<pre><code>Movie.objects.all().filter(genre=self.object)
</code></pre>

python django