aboutsummaryrefslogtreecommitdiffstats
path: root/examples/resourceexample/views.py
blob: f2a7a08a73ef55522b60c7d3b9d2309609130724 (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
from djangorestframework.utils import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status

from resourceexample.forms import MyForm


class ExampleView(View):
    """
    A basic read-only view that points to 3 other views.
    """

    def get(self, request):
        """
        Handle GET requests, returning a list of URLs pointing to
        three other views.
        """
        urls = [reverse('another-example', request, kwargs={'num': num})
                for num in range(3)]
        return Response({"Some other resources": urls})


class AnotherExampleView(View):
    """
    A basic view, that can handle GET and POST requests.
    Applies some simple form validation on POST requests.
    """
    form = MyForm

    def get(self, request, num):
        """
        Handle GET requests.
        Returns a simple string indicating which view the GET request was for.
        """
        if int(num) > 2:
            return Response(status=status.HTTP_404_NOT_FOUND)
        return Response("GET request to AnotherExampleResource %s" % num)

    def post(self, request, num):
        """
        Handle POST requests, with form validation.
        Returns a simple string indicating what content was supplied.
        """
        if int(num) > 2:
            return Response(status=status.HTTP_404_NOT_FOUND)
        return Response("POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT)))