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

查看:491
本文介绍了如何在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-guide/requests/

但是当我使用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)
        ...
        ...
        ...

当我在邮递员的请求正文中传递一些值并打印该值时,它实际上会打印无.

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天全站免登陆