【发布时间】:2012-08-11 14:11:18
【问题描述】:
我目前正在开发使用 Android 作为客户端和 Django 作为 Web 服务器的项目。我决定使用活塞-django 来创建 REST API 身份验证,并且我已按照以下说明进行操作: What is the right way to write a django-piston client? 并编写我自己的处理程序(api/handlers.py)来创建和返回 ApiKey,如下所示:
class ApiKeyhandler(Basehandler):
model = ApiKey
allowed_methods = ('GET', 'POST', 'PUT', 'DELETE')
fields = ('user', 'keys')
def create(self, request):
attrs = self.flatten_dict(request.POST)
if self.exists(**attrs):
return rc.DUPLICATE_ENTRY
else:
apikey = ApiKey(user=request.user)
apikey.save()
return apikey
在 urls.py 中,我为此处理程序使用 HttpBasicAuthentication:
auth = HttpBasicAuthentication(realm="Authentication API")
apikey = Resource(handler=ApiKeyHandler, authentication=auth)
但是当我用http://hurl.it测试它时
This is the response from POST method
谁能告诉我如何为这个问题编写完整的代码或关于这个问题的任何建议?
【问题讨论】:
标签: django api rest django-piston