如何通过urls.py使用DRF引发NotFound异常

时间:2015-08-17 19:07:52

标签: python django django-urls

任何人都可以通过urls.py确认如何使用DRF引发NotFound异常吗?

urlpatterns = [
    url(r'^blog/$', views.page),
    url(r'^$, <?????>),
]

由于

1 个答案:

答案 0 :(得分:1)

你应该在视图中提升它

urls.py:

urlpatterns = [
    url(r'^blog/$', views.page),
    url(r'^$', views.not_found),
]

views.py:

from rest_framework.exceptions import NotFound
from rest_framework.decorators import api_view

@api_view()
def not_found(request):
    raise NotFound('Not Found!')