我正在尝试自动将URL获取到特定的ViewSet。
我尝试使用{%url'api-movement'%},但收到以下错误:
未找到“ API运动”的反向键。 “ api-movement”不是有效的视图函数或模式名称。
我搜索了网络,但找不到真正想要的东西。
viewset.py
class MovementViewSet(viewsets.ModelViewSet):
queryset = Movement.objects.all()
serializer_class = MovementSerializer
router.py
from farm.api.viewsets import *
from rest_framework import routers
router = routers.DefaultRouter()
router.register('movement', MovementViewSet, base_name='api-movement')
urls.py
from django.urls import path, include
from farm.api.responder import *
from farm import views
from farm.router import router
urlpatterns = [
path('api/', include(router.urls)),
]
html摘录:
$.ajaxSetup({
headers: { "X-CSRFToken": '{{csrf_token}}' },
});
$.ajax({
method: 'POST',
url: "{% url 'api-movement' %}",
data: {pk: pk},
success: function (data) {
location.reload();
},
error: function (error_data) {
alert('An error occurred, please reload page and try again.\n\nError: ' + error_data.responseText);
}
});