代码之家  ›  专栏  ›  技术社区  ›  Dhanushka Amarakoon

同一资源的单独POST访问URL

  •  0
  • Dhanushka Amarakoon  · 技术社区  · 10 年前

    我需要为用户创建和用户身份验证创建单独的POST方法

    如: http://localhost:8000/registerUser 它需要电子邮件、姓名和密码来注册用户和另一个url

    如: http://localhost:8000/authenticateUser whcih使用电子邮件和密码对用户进行身份验证

    我可以通过重写“override_url”或“dispatch”方法来实现吗?或者其他方式

    1 回复  |  直到 8 年前
        1
  •  1
  •   Remi Smirra    10 年前

    我想,你要找的是 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
    

    另一种选择是,只创建两个资源(在继承另一个资源时),然后更改 resource_name 在元类中