from django.views.generic import DetailView

from .models import Page


class PageView(DetailView):

    model = Page

    def get_object(self, queryset=None):

        slug = self.kwargs.get('slug')
        if not slug:
            slug = 'index'
        return self.get_queryset().get(slug=slug)

from django.views.generic import DetailView
from django.shortcuts import get_object_or_404

from .models import Page


class PageView(DetailView):

    model = Page

    def get_object(self, queryset=None):

        slug = self.kwargs.get('slug')
        if not slug:
            slug = 'index'
        return get_object_or_404(self.get_queryset(), slug=slug)

Return 404 in case page is not found
Return 404 in case page is not found

  from django.views.generic import DetailView
+ from django.shortcuts import get_object_or_404
  
  from .models import Page
  
  
  class PageView(DetailView):
  
      model = Page
  
      def get_object(self, queryset=None):
  
          slug = self.kwargs.get('slug')
          if not slug:
              slug = 'index'
-         return self.get_queryset().get(slug=slug)
+         return get_object_or_404(self.get_queryset(), slug=slug)
  
Return 404 in case page is not found
## Code Before:
from django.views.generic import DetailView

from .models import Page


class PageView(DetailView):

    model = Page

    def get_object(self, queryset=None):

        slug = self.kwargs.get('slug')
        if not slug:
            slug = 'index'
        return self.get_queryset().get(slug=slug)

## Instruction:
Return 404 in case page is not found
## Code After:
from django.views.generic import DetailView
from django.shortcuts import get_object_or_404

from .models import Page


class PageView(DetailView):

    model = Page

    def get_object(self, queryset=None):

        slug = self.kwargs.get('slug')
        if not slug:
            slug = 'index'
        return get_object_or_404(self.get_queryset(), slug=slug)
