2012-02-22 00:57:36 +04:00
|
|
|
from djangorestframework.reverse import reverse
|
2011-05-24 13:27:24 +04:00
|
|
|
from djangorestframework.views import View
|
2011-02-19 13:47:26 +03:00
|
|
|
from djangorestframework.response import Response
|
|
|
|
from djangorestframework import status
|
|
|
|
|
2011-02-02 02:07:55 +03:00
|
|
|
from resourceexample.forms import MyForm
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
|
2011-06-02 19:03:11 +04:00
|
|
|
class ExampleView(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
2011-06-02 19:03:11 +04:00
|
|
|
A basic read-only view that points to 3 other views.
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def get(self, request):
|
2011-06-02 19:03:11 +04:00
|
|
|
"""
|
2012-02-22 00:12:14 +04:00
|
|
|
Handle GET requests, returning a list of URLs pointing to
|
|
|
|
three other views.
|
2011-06-02 19:03:11 +04:00
|
|
|
"""
|
2012-02-25 22:45:17 +04:00
|
|
|
resource_urls = [reverse('another-example',
|
|
|
|
kwargs={'num': num},
|
|
|
|
request=request)
|
|
|
|
for num in range(3)]
|
|
|
|
return Response({"Some other resources": resource_urls})
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
|
2011-06-02 19:03:11 +04:00
|
|
|
class AnotherExampleView(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
2012-02-07 18:52:15 +04:00
|
|
|
A basic view, that can handle GET and POST requests.
|
2011-06-02 19:03:11 +04:00
|
|
|
Applies some simple form validation on POST requests.
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
2011-06-02 19:03:11 +04:00
|
|
|
form = MyForm
|
2011-02-02 01:37:51 +03:00
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def get(self, request, num):
|
2011-06-02 19:03:11 +04:00
|
|
|
"""
|
|
|
|
Handle GET requests.
|
|
|
|
Returns a simple string indicating which view the GET request was for.
|
|
|
|
"""
|
2011-02-02 01:37:51 +03:00
|
|
|
if int(num) > 2:
|
2012-02-22 00:12:14 +04:00
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2012-02-07 18:52:15 +04:00
|
|
|
return Response("GET request to AnotherExampleResource %s" % num)
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-04-27 21:07:28 +04:00
|
|
|
def post(self, request, num):
|
2011-06-02 19:03:11 +04:00
|
|
|
"""
|
|
|
|
Handle POST requests, with form validation.
|
|
|
|
Returns a simple string indicating what content was supplied.
|
|
|
|
"""
|
2011-02-02 01:37:51 +03:00
|
|
|
if int(num) > 2:
|
2012-02-22 00:12:14 +04:00
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2012-02-07 18:52:15 +04:00
|
|
|
return Response("POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT)))
|