0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Djangoでオブジェクトが見つからなかったら404を表示

Last updated at Posted at 2021-01-25

環境

  • Django 3.0.3

オブジェクト1件を取得

オブジェクトを1件取得もしくは、オブジェクトが見つからなかったら404を表示させるにはget_object_or_404 を使います。

get_object_or_404(klass, *args, **kwargs)
Django のショートカット関数 | Django ドキュメント | Django

from django.shortcuts import get_object_or_404
from apps.products.models Product

class ProductView(LoginRequiredMixin, generic.TemplateView):
    """ 商品詳細ページ """
    model = Product
    template_name = 'products/detail.html'
    raise_exception = True # 403ページの表示
    pk_url_kwarg = "prroduct_id"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['product'] = get_object_or_404(
                    Product,
                    pk=self.kwargs['product_id']
                 )
        return context

複数条件で取得

取得条件が複数ある場合はこのように条件を複数指定することもできます。
filterを使う必要はありません。

from django.shortcuts import get_object_or_404
from apps.products.models Product

class ProductView(LoginRequiredMixin, generic.TemplateView):
    """ 商品詳細ページ """
    model = Product
    template_name = 'products/detail.html'
    raise_exception = True # 403ページの表示
    pk_url_kwarg = "prroduct_id"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['product'] = get_object_or_404(
                    Product,
                    pk=self.kwargs['product_id'],
                    price=10000,
                    title='商品名'
                 )
        return context

オブジェクト複数件を取得

オブジェクトを複数件を取得するにはget_list_or_404 を使います。
オブジェクトが見つからなかったら404を表示します。

get_list_or_404(klass, *args, **kwargs)
Django のショートカット関数 | Django ドキュメント | Django

from django.shortcuts import get_object_or_404
from apps.products.models Product

class ProductsView(LoginRequiredMixin, generic.TemplateView):
    """ 商品リストページ """
    model = Product
    template_name = 'products/index.html'
    raise_exception = True # 403ページの表示
    pk_url_kwarg = "prroduct_id"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['products'] = get_list_or_404(
                    Product,
                    price=10000,
                 )
        return context

複数条件で取得

取得条件が複数ある場合はこのように条件を複数指定することもできます。
get_list_or_404 の場合もfilterを使う必要はありません。

from django.shortcuts import get_list_or_404
from apps.products.models Product

class ProductsView(LoginRequiredMixin, generic.TemplateView):
    """ 商品リストページ """
    model = Product
    template_name = 'products/index.html'
    raise_exception = True # 403ページの表示
    pk_url_kwarg = "prroduct_id"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['products'] = get_list_or_404(
                    Product,
                    price=10000,
                    title='商品名'
                 )
        return context

まとめ

get_object_or_404 はオブジェクトを1件取得。見つからなかった場合は404を表示。複数条件で検索可能。

get_list_or_404 オブジェクトを複数取得。見つからなかった場合は404を表示。複数条件で検索可能。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?