Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django will not redirect to 404 in try except statement

I'm currently in the process of learning the Django library and am stumped because I am receiving a DoesNotExist error (status = 500) instead of a 404 page error, I tried turning debug = False, however all I received was a 500 status page instead.

class CategoryView(generic.ListView):
    model = Category
    template_name = 'rango/category.html'
    allow_empty = False

    try:
        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 = Category.objects.get(name = category_name)
            pages = Page.objects.filter(category = category)
            context['category'] = category
            context['pages'] = pages
            return context   
    except Category.DoesNotExist:
        raise Http404

Traceback:

DoesNotExist at /rango/category/Perl/

Category matching query does not exist.

Traceback: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 114. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view 69. return self.dispatch(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in dispatch 87. return handler(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/list.py" in get 152. context = self.get_context_data() File "/Users/justahack/Documents/Python/tango_with_django_project/rango/views.py" in get_context_data 47. category = Category.objects.get(name = category_name) File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in get 151. return self.get_queryset().get(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get 307. self.model._meta.object_name)

Exception Type: DoesNotExist at /rango/category/Perl/ Exception Value: Category matching query does not exist.

Any help is much appreciated.

like image 562
tkauran Avatar asked Mar 20 '23 22:03

tkauran


1 Answers

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
like image 179
alecxe Avatar answered Apr 01 '23 16:04

alecxe