我正在尝试学习一些 REST。我已经向现有的 Django 应用程序添加了几个视图,以尝试使用 REST 和 JSON 做各种事情。我可以让我的应用程序通过多个视图发送请求的数据,但我似乎无法让它接受 JSON 作为 URL 的一部分。
我创建了一个看起来像这样的视图:
def restCreateEvent(request, key, jsonString): 错误 = checkKey(key)
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')
当我尝试访问以下 url 时,Django debug 抱怨我的 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"}}
现在视图永远不会被调用,所以显然我的 url 是错误的。那么,我的方法在这里完全错误吗?如果不是,我该如何修复网址?