unable to update (PUT) and delete (delete) data in

2019-07-13 19:25发布

问题:

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

回答1:

Make sure you put in the trailing slashes in your request URL.

Right now, since the trailing slash is missing, your request is being auto forwarded by Django to the address which includes the trailing slash - and in that case, the requests are being converted to 'GET' rather than the original 'PUT' or 'DELETE'.

This might be a bug in Django, but you can work around it easily by including the trailing slash.