KeyError at /x/x/ ‘pk’ django

huangapple go评论90阅读模式
英文:

KeyError at /x/x/ 'pk' django

问题

views.py

  1. class MangaView(DetailView):
  2. model = Manga
  3. template_name = 'mangas/manga.html'
  4. context_object_name = 'manga'
  5. slug_field = 'manga_slug'
  6. slug_url_kwarg = 'manga_slug'
  7. def get_context_data(self, **kwargs):
  8. data = super().get_context_data(**kwargs)
  9. manga = get_object_or_404(Manga, id=self.kwargs['pk']) # error line
  10. favorited = False
  11. latered = False
  12. ...
  13. return data

urls.py

  1. urlpatterns = [
  2. path('mangas/', MangasHomeView.as_view(), name='mangas-home'),
  3. path('manga/<slug:manga_slug>/', MangaView.as_view(), name='manga'),
  4. ...
  5. ]

请注意,代码部分没有进行翻译。

英文:

I'm trying to get a DetailView's model's pk in a variable in the get_context_data but I keep getting the error KeyError at /x/x/ 'pk'.

views.py

  1. class MangaView(DetailView):
  2. model = Manga
  3. template_name = &#39;mangas/manga.html&#39;
  4. context_object_name = &#39;manga&#39;
  5. slug_field = &#39;manga_slug&#39;
  6. slug_url_kwarg = &#39;manga_slug&#39;
  7. def get_context_data(self, **kwargs):
  8. data = super().get_context_data(**kwargs)
  9. manga = get_object_or_404(Manga, id=self.kwargs[&#39;pk&#39;]) # error line
  10. favorited = False
  11. latered = False
  12. ...
  13. return data

urls.py

  1. urlpatterns = [
  2. path(&#39;mangas/&#39;, MangasHomeView.as_view(), name=&#39;mangas-home&#39;),
  3. path(&#39;manga/&lt;slug:manga_slug&gt;/&#39;, MangaView.as_view(), name=&#39;manga&#39;),
  4. ...
  5. ]

答案1

得分: 2

你看到的错误是因为尝试通过self.kwargs访问pk值,而实际上你的视图正在使用slug字段和slug URL参数。

尝试使用self.get_object(),因为它会在DetailView中为你提供当前实例,无论是pk还是slug,如下所示:

  1. class MangaView(DetailView):
  2. model = Manga
  3. template_name = 'mangas/manga.html'
  4. context_object_name = 'manga'
  5. slug_field = 'manga_slug'
  6. slug_url_kwarg = 'manga_slug'
  7. def get_context_data(self, **kwargs):
  8. data = super().get_context_data(**kwargs)
  9. manga = self.get_object()
  10. favorited = False
  11. latered = False
  12. ...
  13. return data
英文:

The error you are seeing is caused by trying to access the pk value through self.kwargs, when in fact your view is using a slug field and slug URL parameter.

Try to use self.get_object() as it gives you current instance in DetailView whether it is pk or slug so:

  1. class MangaView(DetailView):
  2. model = Manga
  3. template_name = &#39;mangas/manga.html&#39;
  4. context_object_name = &#39;manga&#39;
  5. slug_field = &#39;manga_slug&#39;
  6. slug_url_kwarg = &#39;manga_slug&#39;
  7. def get_context_data(self, **kwargs):
  8. data = super().get_context_data(**kwargs)
  9. manga = self.get_object()
  10. favorited = False
  11. latered = False
  12. ...
  13. return data

huangapple
  • 本文由 发表于 2023年2月16日 02:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定