无法在django-piston中更新(PUT)和删除(删除)数据 [英] unable to update (PUT) and delete (delete) data in django-piston

查看:149
本文介绍了无法在django-piston中更新(PUT)和删除(删除)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遵循这个教程,例子很棒。 http://weblog.mattdorn。 com / content / restful-web-apps-with-django-piston-and-ext-js /



但是当我自己创建时,添加方法可以,但删除和更新不是。
这里是我的runserver的控制台:

  [16 / Nov / 2011 00:11:17]DELETE / api / phonebooks / 10 HTTP / 1.1301 0 
[16 / Nov / 2011 00:11:17]GET / api / phonebooks / 10 / HTTP / 1.1200 255
[ 11/2011 00:11:23]PUT / api / phonebooks / 12 HTTP / 1.1301 0
[16 / Nov / 2011 00:11:23]GET / api / phonebooks / 12 / HTTP / 1.1200 253

我认为它删除和更新数据,但是它再次调用数据,以便它没有任何变化。当我运行调试到我的handlers.py,它不能进入​​更新方法。

 #handlers.py 
from django.utils import simplejson

来自piston.handler import BaseHandler
来自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)
如果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):
如果不是self.has_model():
返回rc.NOT_IMPLEMENTED

attrs = self.flatten_dict(request.POST)
如果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

我也尝试删除 allowed_methods ,但没有任何反应。任何人都可以给我一个关于我的情况的想法,p>


提前感谢

解决方案

请确保您在请求URL中放入斜线。



现在,由于尾部斜线缺失,您的请求正由Django自动转发到包含尾部斜线的地址,在这种情况下,请求正被转换为GET 而不是原来的PUT或DELETE。



这可能是Django中的一个错误,但您可以通过包含尾部斜杠轻松解决问题。 / p>

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

解决方案

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.

这篇关于无法在django-piston中更新(PUT)和删除(删除)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆