django-piston:正确格式化来自客户端应用程序的 POST 数据

发布于 2024-10-15 20:02:31 字数 2331 浏览 2 评论 0原文

我有一个 Django 应用程序第一次运行 django-piston。

我已经为模型配置了一个处理程序,并且它已针对 GET 和 POST 进行了配置。

GET 工作正常。经过尝试,确实如此。

然而 POST 很糟糕。当我向它发布数据时,我收到此错误,

Responded: Piston/0.2.3rc1 (Django 1.2.4) crash report:

Method signature does not match.

Resource does not expect any parameters.

Exception was: cannot concatenate 'str' and 'dict' objects

我真的不是 Python 专业人士,但一些基本的谷歌搜索显示,这似乎是一个相当通用的 Python TypeError

所以这里有一些代码。

urls.py(相关部分)

auth = DjangoAuthentication()
ad = { 'authentication': auth }

word_handler = Resource(handler = WordHandler, **ad)

urlpatterns = patterns(
    url(r'^word/(?P<word_id>[^/]+)/', word_handler),
    url(r'^words/', word_handler),
)

handlers.py(相关部分)

class WordHandler(BaseHandler):
    allowed_methods = ('GET','POST',)
    model = Word

    fields = ('string', 'id', 'sort_order', ('owner', ('id',)),)

    def read(self, request, word_id = None):
        """
        Read code goes here
        """

    def create(self, request):

        if request.content_type:
            data = request.data

            print("Words Data: " + data)

            existing_words = Word.objects.filter(owner = request.user)

            for word in data['words']:

                word_already_exists = False

                for existing_word in existing_words:

                    if word["string"] == existing_word.string:
                        word_already_exists = True
                        break


                if not word_already_exists:
                    Word(string = word["string"], owner = request.user).save()

            return rc.CREATED

基本上它甚至根本没有进入 create() ,或者至少看起来没有。它只是因上述错误而崩溃,所以我什至看不到它是如何接收数据的。

就为了它,这是我试图发布的数据

{"words":[{"owner":{"id":1,"tags":null,"password":null,"words":null,"email":null,"firstname":null,"lastname":null,"username":null},"id":1,"sort_order":0,"tags":null,"string":"Another Test"},{"owner":{"id":1,"tags":null,"password":null,"words":null,"email":null,"firstname":null,"lastname":null,"username":null},"id":2,"sort_order":0,"tags":null,"string":"Here's a test"},{"owner":null,"id":0,"sort_order":0,"tags":null,"string":"Tampa"}]}

任何信息都会非常有帮助。

I have a Django app running django-piston for the first time.

I've got a handler configured for a model and it's rigged for GET and POST.

GET works fine. Tried and true.

POST however is sucking. When I post data to it, I am getting this error

Responded: Piston/0.2.3rc1 (Django 1.2.4) crash report:

Method signature does not match.

Resource does not expect any parameters.

Exception was: cannot concatenate 'str' and 'dict' objects

I'm really not a pro at Python but some basic Googling reveals that this seems to be a rather generic Python TypeError

So here's some code.

urls.py (relevant parts)

auth = DjangoAuthentication()
ad = { 'authentication': auth }

word_handler = Resource(handler = WordHandler, **ad)

urlpatterns = patterns(
    url(r'^word/(?P<word_id>[^/]+)/', word_handler),
    url(r'^words/', word_handler),
)

handlers.py (relevant parts)

class WordHandler(BaseHandler):
    allowed_methods = ('GET','POST',)
    model = Word

    fields = ('string', 'id', 'sort_order', ('owner', ('id',)),)

    def read(self, request, word_id = None):
        """
        Read code goes here
        """

    def create(self, request):

        if request.content_type:
            data = request.data

            print("Words Data: " + data)

            existing_words = Word.objects.filter(owner = request.user)

            for word in data['words']:

                word_already_exists = False

                for existing_word in existing_words:

                    if word["string"] == existing_word.string:
                        word_already_exists = True
                        break


                if not word_already_exists:
                    Word(string = word["string"], owner = request.user).save()

            return rc.CREATED

Basically it's not even getting to create() at all, or at least it seems not to. It just craps out with the aforementioned error, so I can't even see how it's receiving the data.

And just for the hell of it, here's the data I am trying to POST

{"words":[{"owner":{"id":1,"tags":null,"password":null,"words":null,"email":null,"firstname":null,"lastname":null,"username":null},"id":1,"sort_order":0,"tags":null,"string":"Another Test"},{"owner":{"id":1,"tags":null,"password":null,"words":null,"email":null,"firstname":null,"lastname":null,"username":null},"id":2,"sort_order":0,"tags":null,"string":"Here's a test"},{"owner":null,"id":0,"sort_order":0,"tags":null,"string":"Tampa"}]}

Any information at all would be extremely helpful.

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

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

发布评论

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

评论(1

清醇 2024-10-22 20:02:31

我自己解决了这个问题。

错误的相关原因是 create 方法中的这一行

if request.content_type:

,它的计算结果并不为 True,正如我认为 django-piston 文档的创建者所希望的那样。即使该值是一个带有值的字符串。

删除这一行解决了它。我确信您可以只进行字符串评估。

不太确定他们在那里想什么。

I solved this for myself.

The relevant cause of the error is this line in the create method

if request.content_type:

which does not evaluate as True as I think the creators of the django-piston documentation intended. Even if the value is a string with a value.

Removing this line solved it. And I'm sure you can probably just do a string evaluation.

Not really sure what they were thinking there.

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