django-rest-framework/api-guide/reverse.html

164 lines
11 KiB
HTML
Raw Normal View History

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>
<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="reverse-page">
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-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>
<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>
<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>
<li><a href="http://tomchristie.github.com/django-rest-framework/topics/formoverloading">Browser based PUT, PATCH and DELETE</a></li>
<li><a href="http://tomchristie.github.com/django-rest-framework/topics/browsable-api">Working with the browsable API</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-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">
<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>
</ul>
2012-09-02 00:24:33 +04:00
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
2012-10-05 16:22:18 +04:00
<div class="span3">
2012-10-05 16:22:18 +04:00
<div id="table-of-contents">
<ul class="nav nav-list side-nav well sidebar-nav-fixed">
2012-09-12 13:14:01 +04:00
<li class="main"><a href="#returning-urls">Returning URLs</a></li>
2012-10-02 13:41:26 +04:00
<li><a href="#reverse">reverse</a></li>
<li><a href="#reverse_lazy">reverse_lazy</a></li>
2012-09-02 00:24:33 +04:00
</ul>
</div>
2012-09-02 00:24:33 +04:00
</div>
<div id="main-content" class="span9">
<p><a class="github" href="https://github.com/tomchristie/django-rest-framework/blob/restframework2/rest_framework/reverse.py"><span class="label label-info">reverse.py</span></a></p>
2012-09-12 13:14:01 +04:00
<h1 id="returning-urls">Returning URLs</h1>
2012-09-02 00:24:33 +04:00
<blockquote>
<p>The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface between components.</p>
<p>&mdash; Roy Fielding, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5">Architectural Styles and the Design of Network-based Software Architectures</a></p>
</blockquote>
2012-09-12 13:14:01 +04:00
<p>As a rule, it's probably better practice to return absolute URIs from you Web APIs, such as <code>http://example.com/foobar</code>, rather than returning relative URIs, such as <code>/foobar</code>.</p>
2012-09-02 00:24:33 +04:00
<p>The advantages of doing so are:</p>
<ul>
<li>It's more explicit.</li>
<li>It leaves less work for your API clients.</li>
<li>There's no ambiguity about the meaning of the string when it's found in representations such as JSON that do not have a native URI type.</li>
2012-09-06 00:14:24 +04:00
<li>It makes it easy to do things like markup HTML representations with hyperlinks.</li>
2012-09-02 00:24:33 +04:00
</ul>
<p>REST framework provides two utility functions to make it more simple to return absolute URIs from your Web API.</p>
<p>There's no requirement for you to use them, but if you do then the self-describing API will be able to automatically hyperlink it's output for you, which makes browsing the API much easier.</p>
2012-10-02 13:41:26 +04:00
<h2 id="reverse">reverse</h2>
<p><strong>Signature:</strong> <code>reverse(viewname, request, *args, **kwargs)</code></p>
2012-09-02 00:24:33 +04:00
<p>Has the same behavior as <a href="https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse"><code>django.core.urlresolvers.reverse</code></a>, except that it returns a fully qualified URL, using the request to determine the host and port.</p>
2012-10-02 14:04:30 +04:00
<pre class="prettyprint lang-py"><code>import datetime
from rest_framework.utils import reverse
from rest_framework.views import APIView
2012-09-02 00:24:33 +04:00
2012-10-02 14:04:30 +04:00
class APIRootView(APIView):
2012-09-02 00:24:33 +04:00
def get(self, request):
2012-10-02 14:04:30 +04:00
year = datetime.datetime.now().year
data = {
2012-09-02 00:24:33 +04:00
...
2012-10-02 14:04:30 +04:00
'year-summary-url': reverse('year-summary', request, args=[year])
2012-09-02 00:24:33 +04:00
}
2012-10-02 14:04:30 +04:00
return Response(data)
2012-09-02 00:24:33 +04:00
</code></pre>
2012-10-02 13:41:26 +04:00
<h2 id="reverse_lazy">reverse_lazy</h2>
<p><strong>Signature:</strong> <code>reverse_lazy(viewname, request, *args, **kwargs)</code></p>
2012-09-02 00:24:33 +04:00
<p>Has the same behavior as <a href="https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy"><code>django.core.urlresolvers.reverse_lazy</code></a>, except that it returns a fully qualified URL, using the request to determine the host and port.</p>
</div><!--/span-->
</div><!--/row-->
2012-10-05 16:22:18 +04:00
2012-09-02 00:24:33 +04:00
</div><!--/.fluid-container-->
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<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>
//$('.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>