0

I'm writing a rest API using Django rest framework, I have setup viewsets, serializers and routes in their corresponding files.But when I send a post request to a route it returns POST method not allowed.

Here's what i have done:

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'

Update: Here's I have added users urls.py also:

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')),

]

and here's the restDeployment URL refers to the rest app urls.

When I submit a POST request to http://127.0.0.1:8000/api/users/rest_deployments/ it returns:

{"detail":"Method \"POST\" not allowed."}

what can be a problem?

Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150

0 Answers0