python - Django不会在try except语句中重定向到404

标签 python django django-models django-views

我目前正在学习 Django 库,但我很困惑 因为我收到DoesNotExist错误(状态= 500)而不是404页面错误, 我尝试将 debug = False 设置为 False,但收到的只是 500 状态页。

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

回溯:

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.

非常感谢任何帮助。

最佳答案

问题在于 try/except block 位于方法外部,无法捕获内部的异常。要修复它,请将 try/except 放入方法中:

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

此外,如果对象不存在,还有一种更好的方法抛出 404 - 使用 get_object_or_404()快捷方式:

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

关于python - Django不会在try except语句中重定向到404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22968708/

相关文章:

python - 在 Python 中编码 128 位整数?

python - Bokeh 仅绘制来自网络抓取数据的特定列

python - 将否定文本转换为python中的文本

python - Django Rest API POST 问题

python - Django - 通过 bool 运算对模型进行排序的最佳方式

Django / cometd (推): Least of all evils?

python - 根据 django 网站上的操作显示用户上个月的进度

python - 记住 Django 模型表单上的表单字段值

Django 更新现有模型字段

python - 让 Sprite 随机出现