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のプロではありませんが、いくつかの基本的なグーグルは、これがかなり一般的なPythonTypeErrorのように見えることを明らかにしています
だからここにいくつかのコードがあります。
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"}]}
どんな情報でも非常に役に立ちます。