单独POST acccess网址,我需要为用户创建和用户认证单独POST方法对同一资源
如:http://localhost:8000/registerUser这需要电子邮件,姓名和密码来注册一个用户和另一个URL
例如: http://localhost:8000/authenticateUser whcih需要电子邮件和密码来验证用户
我可以通过覆盖“override_url”或“dispatch”方法来做到这一点吗?或者任何其他方式
单独POST acccess网址,我需要为用户创建和用户认证单独POST方法对同一资源
如:http://localhost:8000/registerUser这需要电子邮件,姓名和密码来注册一个用户和另一个URL
例如: http://localhost:8000/authenticateUser whcih需要电子邮件和密码来验证用户
我可以通过覆盖“override_url”或“dispatch”方法来做到这一点吗?或者任何其他方式
我认为,你要找的是prepend_url
函数,请参阅here。你可以使用这样的:
class AuthenticateUser(Resource)
class Meta:
resource_name = "authenticateUser"
def prepend_urls(self):
#add the cancel url to the resource urls
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/register%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('register'), name="api_authenticate_register"),
]
def register(self, request, **kwargs):
# check request
self.method_check(request, allowed=['post'])
# handle your request here, register user
return self.create_response(request, <some method>)
有了这个,你可以这样调用它:
http://localhost:8000/authenticateUser # to authenticate
http://localhost:8000/authenticateUser/register # to register
另一种选择是,只需创建两个资源(从其他继承),只是更改meta元素中的resource_name
感谢wii试用 –
如何访问请求数据? 'request.data ['email']'给我AttributeError:请求 –
通常,你可以通过request.POST.get('email',None)来访问它' –
为什么不只是创建2个资源? –
当数据相同时,创建2个资源是否有意义? –
authenticateUser做什么? REST身份验证发生在头文件中。 –