2011-02-04 17 views
0

我有第一次運行django-piston的Django應用程序。django-piston:正確格式化來自客戶端應用程序的POST數據

我已經爲模型配置了一個處理程序,並且它爲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類型錯誤

所以這裏有一些代碼。

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 

基本上它甚至沒有得到創建()所有,或在至少它似乎不是。它只是出於上述錯誤,所以我甚至不知道它是如何接收數據的。

而只是爲了它的地獄,這裏是我嘗試後

{"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"}]} 

任何信息將是非常有用的數據。

回答

0

我爲自己解決了這個問題。

誤差的有關原因是該行的創建方法

if request.content_type: 

,因爲我認爲預期的Django活塞文檔的創建者不評估爲True。即使該值是一個帶有值的字符串。

刪除此行解決了它。我相信你可以做一個字符串評估。

不太確定他們在那裏想什麼。

相關問題