aboutsummaryrefslogtreecommitdiffstats
path: root/docs/howto/requestmixin.rst
blob: c0aadb3f767c0d65cf2092186af7d25edc4c4e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Using the enhanced request in all your views
==============================================

This example shows how you can use Django REST framework's enhanced `request` - :class:`request.Request` - in your own views, without having to use the full-blown :class:`views.View` class.

What can it do for you ? Mostly, it will take care of parsing the request's content, and handling equally all HTTP methods ... 

Before
--------

In order to support `JSON` or other serial formats, you might have parsed manually the request's content with something like : ::

    class MyView(View):

        def put(self, request, *args, **kwargs):
            content_type = request.META['CONTENT_TYPE']
            if (content_type == 'application/json'):
                raw_data = request.read()
                parsed_data = json.loads(raw_data)

            # PLUS as many `elif` as formats you wish to support ...

            # then do stuff with your data :
            self.do_stuff(parsed_data['bla'], parsed_data['hoho'])

            # and finally respond something

... and you were unhappy because this looks hackish.

Also, you might have tried uploading files with a PUT request - *and given up* since that's complicated to achieve even with Django 1.3.


After
------

All the dirty `Content-type` checking and content reading and parsing is done for you, and you only need to do the following : ::

    class MyView(MyBaseViewUsingEnhancedRequest):

        def put(self, request, *args, **kwargs):
            self.do_stuff(request.DATA['bla'], request.DATA['hoho'])
            # and finally respond something

So the parsed content is magically available as `.DATA` on the `request` object.

Also, if you uploaded files, they are available as `.FILES`, like with a normal POST request.

.. note:: Note that all the above is also valid for a POST request.


How to add it to your custom views ?
--------------------------------------

Now that you're convinced you need to use the enhanced request object, here is how you can add it to all your custom views : ::

    from django.views.generic.base import View

    from djangorestframework.mixins import RequestMixin
    from djangorestframework import parsers


    class MyBaseViewUsingEnhancedRequest(RequestMixin, View):
        """
        Base view enabling the usage of enhanced requests with user defined views.
        """

        parser_classes = parsers.DEFAULT_PARSERS

        def dispatch(self, request, *args, **kwargs):
            request = self.prepare_request(request)
            return super(MyBaseViewUsingEnhancedRequest, self).dispatch(request, *args, **kwargs)

And then, use this class as a base for all your custom views.

.. note:: you can see this live in the examples.