gpt4 book ai didi

python - Django - 在 get_context_data 内部过滤

转载 作者:行者123 更新时间:2023-12-02 17:24:40 24 4
gpt4 key购买 nike

在 Django 中使用基于类的 View ,我在 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'

我似乎无法弄清楚出了什么问题,我们将不胜感激!

最佳答案

传递给 get_context_datakwargs 参数不包含对象的主键值。不过,您可以从 self.kwargs 获取它:

Movie.objects.all().filter(genre=self.kwargs['pk'])

此外,当您在 get_context_data,因此您也可以使用它来执行以下操作:

Movie.objects.all().filter(genre=self.object)

关于python - Django - 在 get_context_data 内部过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com