KeyError at /x/x/ ‘pk’ django

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

KeyError at /x/x/ 'pk' django

问题

views.py

class MangaView(DetailView):
    model = Manga
    template_name = 'mangas/manga.html'
    context_object_name = 'manga'
    slug_field = 'manga_slug'
    slug_url_kwarg = 'manga_slug'

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)

        manga = get_object_or_404(Manga, id=self.kwargs['pk'])  # error line
        favorited = False
        latered = False
        ...
        return data

urls.py

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

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

英文:

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

class MangaView(DetailView):
    model = Manga
    template_name = &#39;mangas/manga.html&#39;
    context_object_name = &#39;manga&#39;
    slug_field = &#39;manga_slug&#39;
    slug_url_kwarg = &#39;manga_slug&#39;

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)

        manga = get_object_or_404(Manga, id=self.kwargs[&#39;pk&#39;])  # error line
        favorited = False
        latered = False
        ...
        return data

urls.py

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

答案1

得分: 2

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

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

class MangaView(DetailView):
    model = Manga
    template_name = 'mangas/manga.html'
    context_object_name = 'manga'
    slug_field = 'manga_slug'
    slug_url_kwarg = 'manga_slug'

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)

        manga = self.get_object()
        favorited = False
        latered = False
        ...
        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:

class MangaView(DetailView):
    model = Manga
    template_name = &#39;mangas/manga.html&#39;
    context_object_name = &#39;manga&#39;
    slug_field = &#39;manga_slug&#39;
    slug_url_kwarg = &#39;manga_slug&#39;

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)

        manga = self.get_object()
        favorited = False
        latered = False
        ...
        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:

确定