0

我正在使用 Django REST 框架编写一个 REST API,我在其相应的文件中设置了视图集、序列化程序和路由。但是当我向路由发送发布请求时,它返回不允许的 POST 方法。

这是我所做的:

视图集.py

class DocRestViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.DocRestSerializer

路线.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')

序列化程序.py

class DocRestSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = models.DocRestModel
    fields = ('name', 'user', 'serviceName', 'dockerImageURL', 'routing')

网址.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'),
]

视图.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 URL 是指其余的应用程序 url。

当我向它提交 POST 请求时,http://127.0.0.1:8000/api/users/rest_deployments/它会返回:

{"detail":"方法 \"POST\" 不允许。"}

有什么问题?

4

0 回答 0