开发者

django-piston: properly formatting POST data from a client application

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

开发者_JAVA技巧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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜