2012-09-02 00:24:33 +04:00
<!DOCTYPE html>
< html lang = "en" > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< meta charset = "utf-8" >
< title > Django REST framework< / title >
2012-10-05 18:26:53 +04:00
< link href = "http://tomchristie.github.com/django-rest-framework/img/favicon.ico" rel = "icon" type = "image/x-icon" >
2012-09-02 00:24:33 +04:00
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta name = "description" content = "" >
< meta name = "author" content = "" >
<!-- Le styles -->
2012-09-08 23:24:07 +04:00
< link href = "http://tomchristie.github.com/django-rest-framework/css/prettify.css" rel = "stylesheet" >
2012-09-02 00:37:41 +04:00
< link href = "http://tomchristie.github.com/django-rest-framework/css/bootstrap.css" rel = "stylesheet" >
< link href = "http://tomchristie.github.com/django-rest-framework/css/bootstrap-responsive.css" rel = "stylesheet" >
2012-09-13 12:40:09 +04:00
< link href = "http://tomchristie.github.com/django-rest-framework/css/default.css" rel = "stylesheet" >
2012-09-02 00:24:33 +04:00
<!-- Le HTML5 shim, for IE6 - 8 support of HTML5 elements -->
<!-- [if lt IE 9]>
< script src = "http://html5shim.googlecode.com/svn/trunk/html5.js" > < / script >
<![endif]-->
2012-10-01 19:27:59 +04:00
< body onload = "prettyPrint()" class = "views-page" >
2012-09-02 00:24:33 +04:00
2012-10-05 22:27:27 +04:00
< div class = "wrapper" >
2012-09-02 00:24:33 +04:00
< div class = "navbar navbar-inverse navbar-fixed-top" >
< div class = "navbar-inner" >
< div class = "container-fluid" >
2012-09-12 16:12:00 +04:00
< a class = "repo-link btn btn-primary btn-small" href = "https://github.com/tomchristie/django-rest-framework/tree/restframework2" > GitHub< / a >
2012-09-02 00:24:33 +04:00
< a class = "btn btn-navbar" data-toggle = "collapse" data-target = ".nav-collapse" >
< span class = "icon-bar" > < / span >
< span class = "icon-bar" > < / span >
< span class = "icon-bar" > < / span >
< / a >
2012-09-02 00:37:41 +04:00
< a class = "brand" href = "http://tomchristie.github.com/django-rest-framework" > Django REST framework< / a >
2012-09-02 00:24:33 +04:00
< div class = "nav-collapse collapse" >
< ul class = "nav" >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework" > Home< / a > < / li >
2012-09-02 00:24:33 +04:00
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Tutorial < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2012-10-09 15:01:56 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/quickstart" > Quickstart< / a > < / li >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/1-serialization" > 1 - Serialization< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/2-requests-and-responses" > 2 - Requests and responses< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/3-class-based-views" > 3 - Class based views< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/4-authentication-permissions-and-throttling" > 4 - Authentication, permissions and throttling< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/tutorial/5-relationships-and-hyperlinked-apis" > 5 - Relationships and hyperlinked APIs< / a > < / li >
2012-10-10 12:36:47 +04:00
<!-- <li><a href="http://tomchristie.github.com/django - rest - framework/tutorial/6 - resource - orientated - projects">6 - Resource orientated projects</a></li> -->
2012-09-02 00:24:33 +04:00
< / ul >
< / li >
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > API Guide < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/requests" > Requests< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/responses" > Responses< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/views" > Views< / a > < / li >
2012-09-12 13:14:01 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/generic-views" > Generic views< / a > < / li >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/parsers" > Parsers< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/renderers" > Renderers< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/serializers" > Serializers< / a > < / li >
2012-10-05 20:10:33 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/fields" > Serializer fields< / a > < / li >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/authentication" > Authentication< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/permissions" > Permissions< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/throttling" > Throttling< / a > < / li >
2012-10-01 19:27:59 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/pagination" > Pagination< / a > < / li >
2012-09-12 13:14:01 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/content-negotiation" > Content negotiation< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/format-suffixes" > Format suffixes< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/reverse" > Returning URLs< / a > < / li >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/exceptions" > Exceptions< / a > < / li >
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/status-codes" > Status codes< / a > < / li >
2012-09-05 16:05:36 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/api-guide/settings" > Settings< / a > < / li >
2012-09-02 00:24:33 +04:00
< / ul >
< / li >
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Topics < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/csrf" > Working with AJAX and CSRF< / a > < / li >
2012-10-13 18:09:05 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/browser-enhancements" > Browser enhancements< / a > < / li >
2012-10-13 18:35:46 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/browsable-api" > The Browsable API< / a > < / li >
2012-10-08 15:19:26 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/rest-hypermedia-hateoas" > REST, Hypermedia & HATEOAS< / a > < / li >
2012-09-05 16:05:36 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/contributing" > Contributing to REST framework< / a > < / li >
2012-10-08 15:19:26 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/migration" > 2.0 Migration Guide< / a > < / li >
2012-10-17 16:50:08 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/release-notes" > Release Notes< / a > < / li >
2012-09-02 00:37:41 +04:00
< li > < a href = "http://tomchristie.github.com/django-rest-framework/topics/credits" > Credits< / a > < / li >
2012-09-02 00:24:33 +04:00
< / ul >
< / li >
< / ul >
< ul class = "nav pull-right" >
2012-10-09 17:13:19 +04:00
<!-- TODO
2012-09-08 11:03:30 +04:00
< li class = "dropdown" >
< a href = "#" class = "dropdown-toggle" data-toggle = "dropdown" > Version: 2.0.0 < b class = "caret" > < / b > < / a >
< ul class = "dropdown-menu" >
< li > < a href = "#" > Trunk< / a > < / li >
< li > < a href = "#" > 2.0.0< / a > < / li >
< / ul >
< / li >
2012-10-09 17:13:19 +04:00
-->
2012-09-08 11:03:30 +04:00
< / ul >
2012-09-02 00:24:33 +04:00
< / div > <!-- /.nav - collapse -->
< / div >
< / div >
< / div >
2012-10-05 22:27:27 +04:00
< div class = "body-content" >
< div class = "container-fluid" >
< div class = "row-fluid" >
2012-10-05 16:22:18 +04:00
2012-10-05 22:27:27 +04:00
< div class = "span3" >
2012-10-08 15:19:26 +04:00
<!-- TODO
< p style = "margin-top: -12px" >
< a class = "btn btn-mini btn-primary" style = "width: 60px" > « previous< / a >
< a class = "btn btn-mini btn-primary" style = "float: right; margin-right: 8px; width: 60px;" > next » < / a >
< / p >
-->
2012-10-05 22:27:27 +04:00
< div id = "table-of-contents" >
< ul class = "nav nav-list side-nav well sidebar-nav-fixed" >
< li class = "main" > < a href = "#class-based-views" > Class Based Views< / a > < / li >
2012-09-27 19:28:13 +04:00
< li > < a href = "#api-policy-attributes" > API policy attributes< / a > < / li >
< li > < a href = "#api-policy-instantiation-methods" > API policy instantiation methods< / a > < / li >
< li > < a href = "#api-policy-implementation-methods" > API policy implementation methods< / a > < / li >
< li > < a href = "#dispatch-methods" > Dispatch methods< / a > < / li >
2012-10-02 14:04:30 +04:00
< li class = "main" > < a href = "#function-based-views" > Function Based Views< / a > < / li >
2012-09-02 00:24:33 +04:00
2012-10-05 22:27:27 +04:00
< / ul >
< / div >
2012-09-08 11:03:30 +04:00
< / div >
2012-09-02 00:24:33 +04:00
2012-10-05 22:27:27 +04:00
< div id = "main-content" class = "span9" >
< p > < a class = "github" href = "https://github.com/tomchristie/django-rest-framework/blob/restframework2/rest_framework/decorators.py" > < span class = "label label-info" > decorators.py< / span > < / a > < a class = "github" href = "https://github.com/tomchristie/django-rest-framework/blob/restframework2/rest_framework/views.py" > < span class = "label label-info" > views.py< / span > < / a > < / p >
2012-10-02 14:04:30 +04:00
< h1 id = "class-based-views" > Class Based Views< / h1 >
2012-09-09 01:06:49 +04:00
< blockquote >
2012-09-02 00:24:33 +04:00
< p > Django's class based views are a welcome departure from the old-style views.< / p >
< p > — < a href = "http://reinout.vanrees.org/weblog/2011/08/24/class-based-views-usage.html" > Reinout van Rees< / a > < / p >
< / blockquote >
2012-09-27 19:28:13 +04:00
< p > REST framework provides an < code > APIView< / code > class, which subclasses Django's < code > View< / code > class.< / p >
< p > < code > APIView< / code > classes are different from regular < code > View< / code > classes in the following ways:< / p >
< ul >
< li > Requests passed to the handler methods will be REST framework's < code > Request< / code > instances, not Django's < code > HttpRequest< / code > instances.< / li >
< li > Handler methods may return REST framework's < code > Response< / code > , instead of Django's < code > HttpResponse< / code > . The view will manage content negotiation and setting the correct renderer on the response.< / li >
< li > Any < code > APIException< / code > exceptions will be caught and mediated into appropriate responses.< / li >
< li > Incoming requests will be authenticated and appropriate permission and/or throttle checks will be run before dispatching the request to the handler method.< / li >
< / ul >
< p > Using the < code > APIView< / code > class is pretty much the same as using a regular < code > View< / code > class, as usual, the incoming request is dispatched to an appropriate handler method such as < code > .get()< / code > or < code > .post()< / code > . Additionally, a number of attributes may be set on the class that control various aspects of the API policy.< / p >
< p > For example:< / p >
< pre class = "prettyprint lang-py" > < code > class ListUsers(APIView):
"""
View to list all users in the system.
* Requires token authentication.
* Only admin users are able to access this view.
"""
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAdmin,)
def get(self, request, format=None):
"""
Return a list of all users.
"""
2012-10-19 23:00:35 +04:00
usernames = [user.username for user in User.objects.all()]
return Response(usernames)
2012-09-27 19:28:13 +04:00
< / code > < / pre >
< h2 id = "api-policy-attributes" > API policy attributes< / h2 >
< p > The following attributes control the pluggable aspects of API views.< / p >
< h3 id = "renderer_classes" > .renderer_classes< / h3 >
< h3 id = "parser_classes" > .parser_classes< / h3 >
< h3 id = "authentication_classes" > .authentication_classes< / h3 >
< h3 id = "throttle_classes" > .throttle_classes< / h3 >
< h3 id = "permission_classes" > .permission_classes< / h3 >
< h3 id = "content_negotiation_class" > .content_negotiation_class< / h3 >
< h2 id = "api-policy-instantiation-methods" > API policy instantiation methods< / h2 >
< p > The following methods are used by REST framework to instantiate the various pluggable API policies. You won't typically need to override these methods.< / p >
< h3 id = "get_renderersself" > .get_renderers(self)< / h3 >
< h3 id = "get_parsersself" > .get_parsers(self)< / h3 >
< h3 id = "get_authenticatorsself" > .get_authenticators(self)< / h3 >
< h3 id = "get_throttlesself" > .get_throttles(self)< / h3 >
< h3 id = "get_permissionsself" > .get_permissions(self)< / h3 >
< h3 id = "get_content_negotiatorself" > .get_content_negotiator(self)< / h3 >
< h2 id = "api-policy-implementation-methods" > API policy implementation methods< / h2 >
< p > The following methods are called before dispatching to the handler method.< / p >
< h3 id = "check_permissions" > .check_permissions(...)< / h3 >
< h3 id = "check_throttles" > .check_throttles(...)< / h3 >
< h3 id = "perform_content_negotiation" > .perform_content_negotiation(...)< / h3 >
< h2 id = "dispatch-methods" > Dispatch methods< / h2 >
< p > The following methods are called directly by the view's < code > .dispatch()< / code > method.
These perform any actions that need to occur before or after calling the handler methods such as < code > .get()< / code > , < code > .post()< / code > , < code > put()< / code > and < code > .delete()< / code > .< br / >
< / p >
2012-10-05 18:26:53 +04:00
< h3 id = "initialself-request-42args-kwargs" > .initial(self, request, *args, **kwargs)< / h3 >
2012-09-27 19:28:13 +04:00
< p > Performs any actions that need to occur before the handler method gets called.
This method is used to enforce permissions and throttling, and perform content negotiation.< / p >
< p > You won't typically need to override this method.< / p >
< h3 id = "handle_exceptionself-exc" > .handle_exception(self, exc)< / h3 >
< p > Any exception thrown by the handler method will be passed to this method, which either returns a < code > Response< / code > instance, or re-raises the exception.< / p >
< p > The default implementation handles any subclass of < code > rest_framework.exceptions.APIException< / code > , as well as Django's < code > Http404< / code > and < code > PermissionDenied< / code > exceptions, and returns an appropriate error response.< / p >
< p > If you need to customize the error responses your API returns you should subclass this method.< / p >
2012-10-05 18:26:53 +04:00
< h3 id = "initialize_requestself-request-42args-kwargs" > .initialize_request(self, request, *args, **kwargs)< / h3 >
2012-09-27 19:28:13 +04:00
< p > Ensures that the request object that is passed to the handler method is an instance of < code > Request< / code > , rather than the usual Django < code > HttpRequest< / code > .< / p >
< p > You won't typically need to override this method.< / p >
2012-10-05 18:26:53 +04:00
< h3 id = "finalize_responseself-request-response-42args-kwargs" > .finalize_response(self, request, response, *args, **kwargs)< / h3 >
2012-09-27 19:28:13 +04:00
< p > Ensures that any < code > Response< / code > object returned from the handler method will be rendered into the correct content type, as determined by the content negotation.< / p >
< p > You won't typically need to override this method.< / p >
2012-10-02 14:48:45 +04:00
< hr / >
2012-10-02 14:04:30 +04:00
< h1 id = "function-based-views" > Function Based Views< / h1 >
< blockquote >
< p > Saying [that Class based views] is always the superior solution is a mistake.< / p >
< p > — < a href = "http://www.boredomandlaziness.org/2012/05/djangos-cbvs-are-not-mistake-but.html" > Nick Coghlan< / a > < / p >
< / blockquote >
< p > REST framework also gives you to work with regular function based views...< / p >
< p > < strong > [TODO]< / strong > < / p >
2012-10-05 22:27:27 +04:00
< / div > <!-- /span -->
< / div > <!-- /row -->
< / div > <!-- /.fluid - container -->
< / div > <!-- /.body content -->
2012-09-02 00:24:33 +04:00
2012-10-05 18:26:53 +04:00
< div id = "push" > < / div >
2012-10-05 22:27:27 +04:00
< / div > <!-- /.wrapper -->
2012-10-05 18:26:53 +04:00
2012-10-05 22:27:27 +04:00
< footer class = "span12" >
2012-10-05 22:33:52 +04:00
< p > Sponsored by < a href = "http://dabapps.com/" > DabApps< / a > .< / a > < / p >
2012-10-05 22:27:27 +04:00
< / footer >
2012-10-05 18:26:53 +04:00
2012-09-02 00:24:33 +04:00
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
2012-09-08 11:03:30 +04:00
< script src = "http://tomchristie.github.com/django-rest-framework/js/jquery-1.8.1-min.js" > < / script >
2012-10-05 16:22:18 +04:00
< script src = "http://tomchristie.github.com/django-rest-framework/js/prettify-1.0.js" > < / script >
< script src = "http://tomchristie.github.com/django-rest-framework/js/bootstrap-2.1.1-min.js" > < / script >
2012-09-02 00:24:33 +04:00
< script >
2012-09-08 11:03:30 +04:00
//$('.side-nav').scrollspy()
var shiftWindow = function() { scrollBy(0, -50) };
if (location.hash) shiftWindow();
window.addEventListener("hashchange", shiftWindow);
2012-09-12 13:14:01 +04:00
2012-09-17 23:21:26 +04:00
$('.dropdown-menu').on('click touchstart', function(event) {
2012-09-12 13:14:01 +04:00
event.stopPropagation();
});
2012-09-02 00:24:33 +04:00
< / script >
2012-10-01 19:27:59 +04:00
< / body > < / html >