Django - 在get_context_data中过滤

时间:2016-09-23 03:29:32

标签: python django

在Django中使用基于类的视图,我在DetailView内部出现问题。

我想获得的是特定类型的所有电影列表,即:Movie.objects.all().filter(genre=genre_id)

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

我收到此错误:

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

当我尝试用Movie.objects.all().filter(genre=pk)替换Movie.objects.all().filter(genre=kwargs['pk'])时,我得到了:

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'

我似乎无法弄清楚什么是错的,任何帮助都会受到赞赏!

2 个答案:

答案 0 :(得分:3)

传递给kwargs的{​​{1}}参数不包含对象的主键值。你可以从get_context_data得到它:

self.kwargs

此外,当您在Movie.objects.all().filter(genre=self.kwargs['pk']) 内时,您会看到self.object (这是Genre已获取的实例)存在且可以访问,因此您可以使用它来执行以下操作:

get_context_data

答案 1 :(得分:0)

  'all_movies': Movie.objects.all().filter(genre=pk)

你确实没有定义pk。您需要先将pk分配给pk变量。

此外,您不需要包含所有内容:

Movie.objects.filter(genre=pk)