如何在 django rest 框架中访问获取请求数据 [英] How to access get request data in django rest framework

查看:29
本文介绍了如何在 django rest 框架中访问获取请求数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在django rest框架中访问GET请求数据.在文档中,他们提到为了清楚起见,我们建议使用 request.query_params 而不是 Django 的标准 request.GET"

How to access GET request data in django rest framework. In the docs, they have mentioned "For clarity inside your code, we recommend using request.query_params instead of the Django's standard request.GET"

https://www.django-rest-framework.org/api-指南/请求/

但是当我使用 request.query_params.get('some_vaue') 时,即使我在请求正文中传递了数据,它也不会给我任何信息.

But when I use request.query_params.get('some_vaue') it gives me none even though I pass the data in the request body.

示例代码示例:

class TestView(APIView):
    def get(self, request):
        test = request.query_params.get('test')
        print('data',test)
        ...
        ...
        ...

当我在 postman 的请求正文中传递一些值并打印该值时,它实际上打印了 None.

When I pass some value in the body of the request in postman and print the value, it actually prints None.

更新

下面是我的 axios 代码

Below is my axios code

 axios.get(API_URL, {
            headers: {
                'Content-Type': 'application/json'
            },
            params: {
                page_num: 1, 
                test: 'test data'
            }
        })
        .then(res => {
            console.log(res);
        })
        .catch(err => {
            console.log(err.response.data);
        });

重新更新:

出于测试目的,我打印了请求对象,如

For testing purpose I printed out the request object like

print(request.__dict__)

所以它打印出来

{'_request': <WSGIRequest: GET '/api/my api url/?page_num=1&test=test+data'>, 'parsers': [<rest_framework.parsers.JSONParser object at 0x0000016742098128>, <rest_framework.parsers.FormParser object at 0x00000167420980B8>, <rest_framework.parsers.MultiPartParser object at 0x00000167420980F0>], 'authenticators': (), 'negotiator': <rest_framework.negotiation.DefaultContentNegotiation object at 0x0000016742098080>, 'parser_context': {'view': <app_name.views.APIClassName object at 0x0000016742280400>, 'args': (), 'kwargs': {}, 'request': <rest_framework.request.Request object at 0x0000016742107080>, 'encoding': 'utf-8'}, '_data': {}, '_files': <MultiValueDict: {}>, '_full_data': {}, '_content_type': <class 'rest_framework.request.Empty'>, '_stream': None, 'accepted_renderer': <rest_framework.renderers.JSONRenderer object at 0x00000167421070B8>, 'accepted_media_type': 'application/json', 'version': None, 'versioning_scheme': None, '_authenticator': None, '_user': <django.contrib.auth.models.AnonymousUser object at 0x0000016741FFAC88>, '_auth': None}

我可以看到它正在传递数据,但不确定为什么如果我执行 request.data['page_num'] 或任何其他值,它不会获取数据.

I could see that it is passing the data but not sure why if i do request.data['page_num'] or any other value it doesn't get the data.

推荐答案

如果您使用基于类的视图:

If you are using class based views :

POST 在 request.data 中提供数据,在 request.query_params 中提供 GET

POST provides data in request.data and GET in request.query_params

如果您使用基于函数的视图:

If you are using function based views:

request.data 将完成这两种方法的工作.

request.data will do the work for both methods.

axios 不支持使用 get 方法将参数作为正文发送,它将在 url 中附加参数.所以如果你使用 axios,你将不得不使用 query_params

axios does not support sending params as body with get method , it will append params in url. so if you are using axios you will have to use query_params

axios 示例代码:

Axios example code:

axios.get(API_URL, {
            params: {
                testData: 'test data',
                pageNum: 1
            }
        })
        .then(res => {
            console.log(res);
        })
        .catch(err => {
            console.log(err.response.data);
        });

DRF 示例代码:

Class TestView(APIView):
    def get(self, request):
        test_data_var = request.query_params['testData']
        page_num_var = request.query_params['pageNum']

注意:如果您在邮递员中测试它,则将获取请求查询参数放在参数"选项卡中.

Note: If you're testing it in postman then put the get request query params in Params tab.

这篇关于如何在 django rest 框架中访问获取请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆