i just followed this tutorial and the example is great. http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/
but when i create on my own, the add method is ok but the delete and update is not. here is the console of my runserver:
[16/Nov/2011 00:11:17] "DELETE /api/phonebooks/10 HTTP/1.1" 301 0
[16/Nov/2011 00:11:17] "GET /api/phonebooks/10/ HTTP/1.1" 200 255
[16/Nov/2011 00:11:23] "PUT /api/phonebooks/12 HTTP/1.1" 301 0
[16/Nov/2011 00:11:23] "GET /api/phonebooks/12/ HTTP/1.1" 200 253
i think it delete and update the data, but it calls the data again so that it doesn't have any changes. and when i run debug to my handlers.py, it can't go in to the update method.
#handlers.py
from django.utils import simplejson
from piston.handler import BaseHandler
from piston.utils import rc, require_mime, require_extended, validate
from phonebook.phoneapp.models import Phonebook
from phonebook.phoneapp.forms import PhonebookForm
class PhonebookHandler(BaseHandler):
allowed_methods = ('GET', 'DELETE', 'POST', 'PUT')
fields = ('id','fullname','address','age','gender','phonenumber','user')
model = Phonebook
def create(self, request, *args, **kwargs):
if not self.has_model():
return rc.NOT_IMPLEMENTED
attrs = self.flatten_dict(request.POST)
if attrs.has_key('data'):
ext_posted_data = simplejson.loads(request.POST.get('data'))
attrs = self.flatten_dict(ext_posted_data)
try:
inst = self.model.objects.get(**attrs)
return rc.DUPLICATE_ENTRY
except self.model.DoesNotExist:
inst = self.model(**attrs)
inst.save()
return inst
except self.model.MultipleObjectsReturned:
return rc.DUPLICATE_ENTRY
def update(self, request, id):
if not self.has_model():
return rc.NOT_IMPLEMENTED
attrs = self.flatten_dict(request.POST)
if attrs.has_key('data'):
ext_posted_data = simplejson.loads(request.POST.get('data'))
attrs = self.flatten_dict(ext_posted_data)
inst = self.model.objects.get(id=id)
inst.fullname = attrs['fullname']
inst.address = attrs['address']
inst.gender = attrs['gender']
inst.age = attrs['age']
inst.phonebook = attrs['phonebook']
inst.save()
return inst
i have also try to remove the allowed_methods
but nothing happens.
do anyone can give an idea about my situation? thanks in advance