How do I raise a Response Forbidden in django

前端 未结 4 1128
情深已故
情深已故 2020-12-12 23:09

I\'d like to do the following:

raise HttpResponseForbidden()

But I get the error:

exceptions must be old-style classes or d         


        
4条回答
  •  时光取名叫无心
    2020-12-12 23:33

    if you want to raise an exception you can use:

    from django.core.exceptions import PermissionDenied
    
    def your_view(...):
        raise PermissionDenied()
    

    It is documented here :

    https://docs.djangoproject.com/en/stable/ref/views/#the-403-http-forbidden-view

    As opposed to returing HttpResponseForbidden, raising PermissionDenied causes the error to be rendered using the 403.html template, or you can use middleware to show a custom "Forbidden" view.

提交回复
热议问题