The problem is that try/except block is outside the method and it cannot catch the exception inside. To fix it, put try/except into the method:
def get_context_data(self, *args, **kwargs):
context = super(CategoryView, self).get_context_data(*args, **kwargs)
category_name = decode_url(self.kwargs['category_name_url'])
# HERE
try:
category = Category.objects.get(name = category_name)
except Category.DoesNotExist:
raise Http404
pages = Page.objects.filter(category = category)
context['category'] = category
context['pages'] = pages
return context
Also, there is a nicer way to throw 404
if the object doesn't exist - use get_object_or_404() shortcut:
def get_context_data(self, *args, **kwargs):
context = super(CategoryView, self).get_context_data(*args, **kwargs)
category_name = decode_url(self.kwargs['category_name_url'])
category = get_object_or_404(Category, name = category_name)
pages = Page.objects.filter(category = category)
context['category'] = category
context['pages'] = pages
return context