Django urlpatterns 正则表达式不精确,得到 404?
Django urlpatterns regex is imprecise, getting 404?
我正在处理 Django API URL,并尝试识别这种类型的 HTTP 请求:
DELETE http://localhost:8000/api/unassigned_events/dddd-dd-dd/d or dd/
- d 代表数字,同时将每个扇区保存在一个参数中。
例如
DELETE http://localhost:8000/api/unassigned_events/2019-06-20/1/
我的正则表达式路径表达式是:
path(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete')
HTTP 请求是上面给出的示例,但我收到了 404 错误,而不是视图的功能。
这里是要调用的查看方法:
class UnassignedClassRequests(APIView):
@staticmethod
def delete(request):
UnassignedEvents.objects.filter(date=request.date, cls_id=request.cls_id).delete()
return HttpResponse(status=status.HTTP_201_CREATED)
我遇到的错误 Chrome:
删除 http://localhost:8000/api/unassigned_events/2019-06-20/1/ 404(未找到)。
我也为路径尝试了这个正则表达式,但没有成功:
path(r'^api/unassigned_events/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$' UnassignedClassRequests.as_view(), name='delete')
我做错了什么?
Django 的 path(..)
[Django-doc] does not use regular expression syntax. You can use re_path(..)
[Django-doc] 为此:
from django.urls import <b>re_path</b>
urlpatterns = [
<b>re_path</b>(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete'),
# ...
]
我正在处理 Django API URL,并尝试识别这种类型的 HTTP 请求:
DELETE http://localhost:8000/api/unassigned_events/dddd-dd-dd/d or dd/
- d 代表数字,同时将每个扇区保存在一个参数中。
例如
DELETE http://localhost:8000/api/unassigned_events/2019-06-20/1/
我的正则表达式路径表达式是:
path(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete')
HTTP 请求是上面给出的示例,但我收到了 404 错误,而不是视图的功能。
这里是要调用的查看方法:
class UnassignedClassRequests(APIView):
@staticmethod
def delete(request):
UnassignedEvents.objects.filter(date=request.date, cls_id=request.cls_id).delete()
return HttpResponse(status=status.HTTP_201_CREATED)
我遇到的错误 Chrome:
删除 http://localhost:8000/api/unassigned_events/2019-06-20/1/ 404(未找到)。
我也为路径尝试了这个正则表达式,但没有成功:
path(r'^api/unassigned_events/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$' UnassignedClassRequests.as_view(), name='delete')
我做错了什么?
Django 的 path(..)
[Django-doc] does not use regular expression syntax. You can use re_path(..)
[Django-doc] 为此:
from django.urls import <b>re_path</b>
urlpatterns = [
<b>re_path</b>(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete'),
# ...
]