我有一个第一次运行django-piston的Django应用程序。
我为一个模型配置了一个处理程序,它适用于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
基本上它甚至根本没有创建(),或者至少它似乎没有。它只是解决了上述错误,所以我甚至看不到它是如何接收数据的。
只是为了它的地狱,这是我试图发布的数据
{"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 :(得分:0)
我为自己解决了这个问题。
错误的相关原因是create method
中的这一行if request.content_type:
不评估为True
,因为我认为django-piston文档的创建者打算。即使值是带有值的字符串。
删除此行解决了它。我相信你可以做一个字符串评估。
不确定他们在那里想什么。