小编tka*_*ran的帖子

Django不会在try除外语句中重定向到404

我目前正在学习Django库,并且感到很沮丧,因为我收到了DidsNotExist错误(状态= 500)而不是404页面错误,我尝试将debug = 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
Run Code Online (Sandbox Code Playgroud)

追溯:

不存在于/ rango / category / Perl /

类别匹配查询不存在。

追溯:get_response 114中的文件“ /Library/Python/2.7/site-packages/django/core/handlers/base.py”。response = wrapd_callback(request,* callback_args,** callback_kwargs)File“ /Library/Python/2.7 /site-packages/django/views/generic/base.py”在视图69中。return self.dispatch(request,* args,** kwargs)File“ /Library/Python/2.7/site-packages/django/views/派发87中的“ generic / base.py”。返回处理程序(请求,* args,** kwargs)在152.上下文中的文件“ /Library/Python/2.7/site-packages/django/views/generic/list.py”。 = get_context_data …

python django django-models django-views

2
推荐指数
1
解决办法
2193
查看次数

标签 统计

django ×1

django-models ×1

django-views ×1

python ×1