check it the suffix needs to be:
RestangularProvider.setRequestSuffix('/?format=json');
otherwise django was tossing a redirect backed to the client, and while it worked - it was not correct.
The last change is to add:
RestangularProvider.setRequestSuffix('?format=json');
in your module definition, and to remove it from the service definition. This way restangular can build the urls in a way that is expected by the django rest framework back-end. Therefore a service should be setup on the JS side as:
var spaceJam = angular.module('spaceJam', ['ui.bootstrap', 'restangular'])
.config(function (RestangularProvider) {
RestangularProvider.setRequestSuffix('?format=json');
RestangularProvider.setResponseExtractor(function (response, operation) {
// blah
return response ;
})
})
spaceJam.factory('EventsResource', function (Restangular) {
return Restangular.all('events')
})
injected into your controllers and accessed as a list:
EventsResource.getList().then(function(events){
$scope.events = events
})
and to remove an item (fire the HTTP Delete):
$scope.events[someRandomIndex].remove()
and on the server side, you need to make sure the ID is being returned. In django rest framework, this includes a solutions like the one below where a mixin is used to include the id.
Thanks for anybody who has put thought into this.
Making progress - apparently the url was being returned instead of the id. This link http://abesto.net/django-rest-framework-add-id-to-in-hyperlinkedmodelserializer/ outlines adding a mixin o the the serializer that adds the id.in case the link doesn't work in the future, here is the relevant snippet:
class WithPkMixin(object):
def get_pk_field(self, model_field):
return self.get_field(model_field)
class EventSerializer(WithPkMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = Event
I think the issue is now in my factory method of my resource / restangular resource definition as the Id is appended in the wrong place (i think because I was hacking originally):
spaceJam.factory('EventsResource', function (Restangular) {
return Restangular.all('events/?format=json')
})
so the string '/?format=json' needs to be removed, and added back in at some other point.
Update looks like the method being sent is to root node. I am using angularjs and restangular my javascript looks like:
spaceJam.factory('EventsResource', function (Restangular) {
return Restangular.all('events/?format=json')
})
var CalendarListCtrl = function($scope, $timeout, EventsResource) {
var timeout = 10000
$scope.events = []
$scope.refresh = function (){
EventsResource.getList().then(function(events){
$scope.events = events
})
}
var asychUpdate = function(){
$scope.refresh()
//$timeout(asychUpdate, timeout);
}
asychUpdate()
$scope.delete = function(urlToDelete) {
var eventToDelete = _.find($scope.events, function(one) {
return one.url === urlToDelete;
});
eventToDelete.remove();
$scope.refresh()
}
}
so while the remove method looks to be documented as the way to remove an item from the list - it doesn't seem to have the proper URL.
I am learning django / python as I build a site for a non-profit I am associated with. I am able to issue the gets for the list / array of elements. The "add" (post) method works, and now I am trying to hook up the delete. My server response is :
{"detail": "Method 'DELETE' not allowed."}
So I assume that i need to permission that verb - but I have no idea where. I am using the DJANGO REST Framework. I haven't been able to locate this on google, so I am askng the kind and knowledgeable folks here. Thank you in advance, and please let me know if I need to post more code.
urls.py
router = routers.DefaultRouter()
router.register(r'events', views.EventViewSet)
urlpatterns = patterns('',
(r'^', include(router.urls)),
)
models.py
class Event(models.Model):
title = models.CharField(max_length=200)
recurring = models.BooleanField()
day = models.CharField(max_length=20, blank=True)
date = models.DateField(null=True, blank=True)
time = models.TimeField()
description = models.CharField(max_length=500)
venue = models.CharField(max_length=200, blank=True)
venueAddress = models.CharField(max_length=200, blank=True)
venueCity = models.CharField(max_length=200, blank=True)
serializers.py
class EventSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Event
views.py
class EventViewSet(viewsets.ModelViewSet):
paginate_by = None
queryset = Event.objects.all()
serializer_class = EventSerializer