Parameters URL with DRF routers

痞子三分冷 提交于 2021-01-29 14:26:13

问题


I'm using Django Rest Framework for created a API. In this project i want to capture parameters in the URL. For example i want to capture the username and password of a user and my idea is like this:

http://localhost:8000/accounts/login/?unsername=username&password=password

But i cant, I' usin routers and django-filter, but i cant get the parameters url. My project files are there:

view.py:

class AccountsData(viewsets.ModelViewSet):
    queryset = models.UserData.objects.all()
    serializer_class = serializers.AccountsDataSerializer
    permission_classes = (IsAuthenticated,)
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = ['username', 'password']

    lookup_url_kwarg = 'username'

    @action(methods=['get'], detail=True, url_name='login', url_path='login')
    def login(self, request, pk=None):
        return Response({"Login successfully"}, 200)

urls.py:

from api import views

router = routers.SimpleRouter()
router.register(r'accounts', views.AccountsData)

回答1:


Request query parameters have nothing to do with routing, they are passed with the request independently of how you configure the route. You have access to them in request.query_params, for example, request.query_params.get('username') would get the value of the username parameter.

Being said that, your idea has a terrible mistake: password or any kind of confidential data should NEVER go in query parameters, you should use an http verb that carries the data in its body (POST, for example).



来源:https://stackoverflow.com/questions/63004267/parameters-url-with-drf-routers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!