import os

from django.views.generic import TemplateView


class ArticleView(TemplateView):

    def get_template_names(self):
        article_name = self.kwargs.get('article_name', 'notfound')
        return [os.path.join('blog', 'blog', article_name, 'index.html')]

import os

from django.views.generic import TemplateView
from django.template.base import TemplateDoesNotExist
from django.http import Http404
from django.template.loader import get_template


class ArticleView(TemplateView):

    def get_template_names(self):
        article_name = self.kwargs.get('article_name', 'notfound')
        template = os.path.join('blog', 'blog', article_name, 'index.html')

        try:
            get_template(template)
        except TemplateDoesNotExist:
            raise Http404

        return [template]

Make sure that missing blog pages don't 500
Make sure that missing blog pages don't 500

  import os
  
  from django.views.generic import TemplateView
+ from django.template.base import TemplateDoesNotExist
+ from django.http import Http404
+ from django.template.loader import get_template
  
  
  class ArticleView(TemplateView):
  
      def get_template_names(self):
          article_name = self.kwargs.get('article_name', 'notfound')
-         return [os.path.join('blog', 'blog', article_name, 'index.html')]
+         template = os.path.join('blog', 'blog', article_name, 'index.html')
  
+         try:
+             get_template(template)
+         except TemplateDoesNotExist:
+             raise Http404
+ 
+         return [template]
+ 
Make sure that missing blog pages don't 500
## Code Before:
import os

from django.views.generic import TemplateView


class ArticleView(TemplateView):

    def get_template_names(self):
        article_name = self.kwargs.get('article_name', 'notfound')
        return [os.path.join('blog', 'blog', article_name, 'index.html')]

## Instruction:
Make sure that missing blog pages don't 500
## Code After:
import os

from django.views.generic import TemplateView
from django.template.base import TemplateDoesNotExist
from django.http import Http404
from django.template.loader import get_template


class ArticleView(TemplateView):

    def get_template_names(self):
        article_name = self.kwargs.get('article_name', 'notfound')
        template = os.path.join('blog', 'blog', article_name, 'index.html')

        try:
            get_template(template)
        except TemplateDoesNotExist:
            raise Http404

        return [template]
