0
我想學習一點REST。我爲現有的Django應用程序添加了多個視圖,以嘗試使用REST和JSON執行各種操作。我能夠讓我的應用通過多個視圖發送請求的數據,但我似乎無法讓它接受JSON作爲URL的一部分。正確的Django URL配置閱讀JSON
我創建看起來像這樣的視圖:
DEF restCreateEvent(請求,鍵,jsonString): 誤差= checkKey(鍵)
if errors == None:
eventJson = json.loads(jsonString)
eventInfo = eventJson['event']
title = eventInfo['title']
description = eventInfo['description']
locationInfo = eventInfo['location']
place = locationInfo['place_name']
street = locationInfo['street_address']
city = locationInfo['city']
state = locationInfo['state']
zip = locationInfo['zip']
event = models.Event()
event.title = title
event.description = description
event.street_address = street
event.place_name = place
event.city = city
event.state = state
event.zip = zip
event.save()
else:
return errors
然而,我可以;噸似乎獲取URL正確的,這裏是我現在有:
(r'^events/rest/create/(?P<key>\d+)/(?P<jsonString>.+)', 'events.views.restCreateEvent')
當我試圖訪問以下網址,Django的調試抱怨說,沒有我的URL匹配。
http://127.0.0.1:8000/events/rest/33456/create/{"title":"test","description":"this is a test","location":{"place_name":"somewhere","street_address":"123 main","city":"pittsburgh","state":"pa","zip":"11111"}}
現在的觀點永遠不會被調用,所以很明顯我的網址是錯誤的。那麼,我的方法在這裏完全錯了嗎?如果不是我如何解決網址?
我從未真正嘗試將任何類型的數據發送到應用程序。看來我正在採取錯誤的做法。 – Jon 2012-08-04 17:50:27