无法更新(PUT)和删除(delete)django-piston 中的数据

发布于 2024-12-16 00:49:37 字数 2448 浏览 2 评论 0原文

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

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

[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

我认为它删除并更新了数据,但它再次调用数据,以便它没有任何更改。当我对 handlers.py 运行调试时,它无法进入更新方法。

#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

我也尝试删除 allowed_methods 但没有任何反应。

有人可以了解我的情况吗? 提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-12-23 00:49:37

确保在请求 URL 中添加尾部斜杠。

现在,由于缺少尾部斜杠,您的请求将被 Django 自动转发到包含尾部斜杠的地址 - 在这种情况下,请求将被转换为“GET”,而不是原始的“PUT”或“删除'。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文