Django REST 框架未获取 API 端点
Django REST framework not picking up API endpoint
我正在尝试重构 API 以使用 Django REST framework。我已将 urls.py
更改为以下内容(仍在 Django 1.11 中):
from django.conf.urls import url
from . import views
from rest_framework import routers, serializers, viewsets
from .serializers import SessionSerializer
from .viewsets import SessionViewSet
router = routers.DefaultRouter()
router.register(r'^api/v1\.0/feedback/$', SessionViewSet)
urlpatterns = [
url(r'^$', views.index),
url(r'^api/v1\.0/profile/$', views.get_profile),
url(r'^api/v1\.0/update_profile/$', views.update_profile),
url(r'^api/v1\.0/update_password/$', views.update_password),
url(r'^api/v1\.0/sessions/$', views.get_session),
url(r'^api/v1\.0/user/$', views.get_user),
url(r'^api/v1\.0/sessions/send_schedule_request/$', views.send_schedule_request),
url(r'^api/v1\.0/sessions/confirm_session_time/$', views.confirm_session_time),
url(r'^api/v1\.0/password_reset/$', views.password_reset),
url(r'^api/v1\.0/me/apn/$', views.save_apn),
url(r'^api/v1\.0/confirm_activation_code$', views.confirm_activation_code),
url(r'^api/v1\.0/update_user_via_activation_code$', views.update_user_via_activation_code),
url(r'^api/v1\.0/questions/$', views.get_questions),
url(r'^api/v1\.0/answers/$', views.save_answers),
# url(r'^api/v1\.0/feedback/$', views.record_feedback),
url(r'^session_types/$', views.session_types),
url(r'^send_custom_notification/(?P<id>\d+)/$', views.send_custom_notification, name='send_custom_notification'),
url(r'^admin/lucy_web/send_session_time_notifications/(?P<user_id>\d+)/(?P<session_id>\d+)/$', views.send_session_time_notifications, name='send_session_time_notifications'),
url(r'^admin/lucy_web/app_activate/(?P<id>\d+)/$', views.app_activate, name='app_activate'),
url(r'^admin/lucy_web/create_activation_code/(?P<id>\d+)/$', views.create_activation_code, name='create_activation_code'),
]
但是,如果我 python manage.py runserver
并转到 localhost:8000/api/v1.0/feedback/
,我会收到 404 错误响应:
似乎 r'^api/v1\.0/feedback/
端点没有被拾取,即使它作为参数传递给 router.register()
。知道为什么这不起作用吗?
您还没有像这样将默认路由器 url 添加回 urlpatterns 变量,
urlpatterns = [...]
urlpatterns += router.urls
看看用法部分here
我正在尝试重构 API 以使用 Django REST framework。我已将 urls.py
更改为以下内容(仍在 Django 1.11 中):
from django.conf.urls import url
from . import views
from rest_framework import routers, serializers, viewsets
from .serializers import SessionSerializer
from .viewsets import SessionViewSet
router = routers.DefaultRouter()
router.register(r'^api/v1\.0/feedback/$', SessionViewSet)
urlpatterns = [
url(r'^$', views.index),
url(r'^api/v1\.0/profile/$', views.get_profile),
url(r'^api/v1\.0/update_profile/$', views.update_profile),
url(r'^api/v1\.0/update_password/$', views.update_password),
url(r'^api/v1\.0/sessions/$', views.get_session),
url(r'^api/v1\.0/user/$', views.get_user),
url(r'^api/v1\.0/sessions/send_schedule_request/$', views.send_schedule_request),
url(r'^api/v1\.0/sessions/confirm_session_time/$', views.confirm_session_time),
url(r'^api/v1\.0/password_reset/$', views.password_reset),
url(r'^api/v1\.0/me/apn/$', views.save_apn),
url(r'^api/v1\.0/confirm_activation_code$', views.confirm_activation_code),
url(r'^api/v1\.0/update_user_via_activation_code$', views.update_user_via_activation_code),
url(r'^api/v1\.0/questions/$', views.get_questions),
url(r'^api/v1\.0/answers/$', views.save_answers),
# url(r'^api/v1\.0/feedback/$', views.record_feedback),
url(r'^session_types/$', views.session_types),
url(r'^send_custom_notification/(?P<id>\d+)/$', views.send_custom_notification, name='send_custom_notification'),
url(r'^admin/lucy_web/send_session_time_notifications/(?P<user_id>\d+)/(?P<session_id>\d+)/$', views.send_session_time_notifications, name='send_session_time_notifications'),
url(r'^admin/lucy_web/app_activate/(?P<id>\d+)/$', views.app_activate, name='app_activate'),
url(r'^admin/lucy_web/create_activation_code/(?P<id>\d+)/$', views.create_activation_code, name='create_activation_code'),
]
但是,如果我 python manage.py runserver
并转到 localhost:8000/api/v1.0/feedback/
,我会收到 404 错误响应:
似乎 r'^api/v1\.0/feedback/
端点没有被拾取,即使它作为参数传递给 router.register()
。知道为什么这不起作用吗?
您还没有像这样将默认路由器 url 添加回 urlpatterns 变量,
urlpatterns = [...]
urlpatterns += router.urls
看看用法部分here