Skip to main content
 首页 » 编程设计

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

2025年05月04日16wuhuacong

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

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

谢谢

请您参考如下方法:

你应该在 View 中提出它

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!')