How do I create a login API using Django Rest Framework?

前端 未结 4 1923
温柔的废话
温柔的废话 2020-12-22 17:49

I want to create a login api (or use an existing one if it is already pre-bundled) using django rest framework. However, I\'m completely at a loss. Whenever I send a post

4条回答
  •  长情又很酷
    2020-12-22 18:12

    If you want something like this I do the same thing however I use Token authentication.

    Check out their token page here

    This may not be what you want but the way I do it is (since I'm using it as a rest api endpoints for mobile clients)

    I can do my url localhost:8000/api/users/ -H Authorization : Token A browser could then use the regular login page that you create at the provided rest framework url

    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')

    and to get tokens for 'login-less' navigation

    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token')

    Then if you make calls and such you can pass the authorization tokens. Of course this is just how I do it and it's probably not the most efficient way but my goal was to create a way that I can provide users with session authentication for browsers and mobile access via tokens.

    Then in your views.py make sure you add the authentication requirements for that view. Almost the same as session authentication section

    permission_classes = (permissions.IsAdminUser,)

    but also include

    authentication_classes = (authentication.TokenAuthentication,)

    I hope this helps but if not, good luck on your search.

提交回复
热议问题