我正在使用Django rest框架编写一个rest API,我在相应的文件中设置了视图集,序列化程序和路由。但是当我向路由发送一个post请求时,它返回POST方法是不允许的。
这就是我所做的:
viewsets.py
class DocRestViewSet(viewsets.ModelViewSet):
serializer_class = serializers.DocRestSerializer
routes.py
router = routers.DefaultRouter()
router.register(r'deployments', viewsets.DeploymentViewSet, base_name='rest_deployments')
router.register(r'rest_deployments', viewsets.DocRestViewSet, base_name='docrest_deployments')
serializers.py
class DocRestSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.DocRestModel
fields = ('name', 'user', 'serviceName', 'dockerImageURL', 'routing')
urls.py
urlpatterns = [
url(r'^create/new$', views.DeploymentView.as_view(), name='new-rest-dep'),
url(r'^rest/$', views.DeploymentView.as_view(), name='doc-rest-dep'),
]
views.py
class DocRestView(LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs):
return 'Post req received for docrest'
更新:此处我已添加用户 urls.py :
urlpatterns = [
url(r'^dashboard/', include('gui.urls')),
url(r'^instance/', include('instances.urls')),
url(r'^images/', include(image_urls)),
url(r'^restDeployment/', include('rest.urls')),
url(r'^dashboard/$', views.Dashboard, name='dashboard'),
url(r'^logout/$', views.LogoutView.as_view(), name='logout'),
url(r'^signup/$', views.SignUpView.as_view(), name='signup'),
url(r'^profile/$', views.ProfileView.as_view(), name='profile'),
url(r'^login/', auth_views.login, name='login'),
url(r'^deploy/docker/', include(auth_urls), name='authGoogle'),
url(r'^deploy/', include(auth_urls), name='awd'),
url('^', include('django.contrib.auth.urls')),
]
此处 restDeployment 网址指的是其他应用网址。
当我向 http://127.0.0.1:8000/api/users/rest_deployments/
提交POST请求时,它会返回:
{" detail":" Method \" POST \"不允许。"}
什么可能是个问题?