2016-01-14 43 views
0

單獨POST acccess網址,我需要爲用戶創建和用戶認證單獨POST方法對同一資源

如:http://localhost:8000/registerUser這需要電子郵件,姓名和密碼來註冊一個用戶和另一個URL

例如: http://localhost:8000/authenticateUser whcih需要電子郵件和密碼來驗證用戶

我可以通過覆蓋「override_url」或「dispatch」方法來做到這一點嗎?或者任何其他方式

+1

爲什麼不只是創建2個資源? –

+0

當數據相同時,創建2個資源是否有意義? –

+0

authenticateUser做什麼? REST身份驗證發生在頭文件中。 –

回答

1

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

+0

感謝wii試用 –

+0

如何訪問請求數據? 'request.data ['email']'給我AttributeError:請求 –

+0

通常,你可以通過request.POST.get('email',None)來訪問它' –