diff options
| author | Tom Christie | 2014-11-03 11:21:29 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-11-03 11:21:29 +0000 |
| commit | dd14c6c88ba210bac8349041b8db486576539249 (patch) | |
| tree | 81b1e22fb8080a8d3915ae6a5db95edf87a3bfb6 /tutorial | |
| parent | ce165805481988e95887bf62c45cd616af5bba0f (diff) | |
| download | django-rest-framework-dd14c6c88ba210bac8349041b8db486576539249.tar.bz2 | |
Latest docs release
Diffstat (limited to 'tutorial')
| -rw-r--r-- | tutorial/1-serialization.html | 13 | ||||
| -rw-r--r-- | tutorial/2-requests-and-responses.html | 9 | ||||
| -rw-r--r-- | tutorial/3-class-based-views.html | 4 | ||||
| -rw-r--r-- | tutorial/4-authentication-and-permissions.html | 4 | ||||
| -rw-r--r-- | tutorial/5-relationships-and-hyperlinked-apis.html | 10 | ||||
| -rw-r--r-- | tutorial/6-viewsets-and-routers.html | 12 |
6 files changed, 27 insertions, 25 deletions
diff --git a/tutorial/1-serialization.html b/tutorial/1-serialization.html index 9c50a584..94145642 100644 --- a/tutorial/1-serialization.html +++ b/tutorial/1-serialization.html @@ -256,9 +256,9 @@ cd tutorial ) </code></pre> <p>We also need to wire up the root urlconf, in the <code>tutorial/urls.py</code> file, to include our snippet app's URLs.</p> -<pre class="prettyprint lang-py"><code>urlpatterns = patterns('', +<pre class="prettyprint lang-py"><code>urlpatterns = [ url(r'^', include('snippets.urls')), -) +] </code></pre> <p>Okay, we're ready to roll.</p> <h2 id="creating-a-model-to-work-with">Creating a model to work with</h2> @@ -459,11 +459,12 @@ def snippet_detail(request, pk): </code></pre> <p>Finally we need to wire these views up. Create the <code>snippets/urls.py</code> file:</p> <pre class="prettyprint lang-py"><code>from django.conf.urls import patterns, url +from snippets import views -urlpatterns = patterns('snippets.views', - url(r'^snippets/$', 'snippet_list'), - url(r'^snippets/(?P<pk>[0-9]+)/$', 'snippet_detail'), -) +urlpatterns = [ + url(r'^snippets/$', views.snippet_list), + url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail), +] </code></pre> <p>It's worth noting that there are a couple of edge cases we're not dealing with properly at the moment. If we send malformed <code>json</code>, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.</p> <h2 id="testing-our-first-attempt-at-a-web-api">Testing our first attempt at a Web API</h2> diff --git a/tutorial/2-requests-and-responses.html b/tutorial/2-requests-and-responses.html index 60acbcf7..8add5464 100644 --- a/tutorial/2-requests-and-responses.html +++ b/tutorial/2-requests-and-responses.html @@ -294,11 +294,12 @@ def snippet_detail(request, pk): <p>Now update the <code>urls.py</code> file slightly, to append a set of <code>format_suffix_patterns</code> in addition to the existing URLs.</p> <pre class="prettyprint lang-py"><code>from django.conf.urls import patterns, url from rest_framework.urlpatterns import format_suffix_patterns +from snippets import views -urlpatterns = patterns('snippets.views', - url(r'^snippets/$', 'snippet_list'), - url(r'^snippets/(?P<pk>[0-9]+)$', 'snippet_detail'), -) +urlpatterns = [ + url(r'^snippets/$', views.snippet_list), + url(r'^snippets/(?P<pk>[0-9]+)$', views.snippet_detail), +] urlpatterns = format_suffix_patterns(urlpatterns) </code></pre> diff --git a/tutorial/3-class-based-views.html b/tutorial/3-class-based-views.html index 83ba088e..c8a1d728 100644 --- a/tutorial/3-class-based-views.html +++ b/tutorial/3-class-based-views.html @@ -263,10 +263,10 @@ class SnippetList(APIView): from rest_framework.urlpatterns import format_suffix_patterns from snippets import views -urlpatterns = patterns('', +urlpatterns = [ url(r'^snippets/$', views.SnippetList.as_view()), url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()), -) +] urlpatterns = format_suffix_patterns(urlpatterns) </code></pre> diff --git a/tutorial/4-authentication-and-permissions.html b/tutorial/4-authentication-and-permissions.html index ea26e60a..f72c8930 100644 --- a/tutorial/4-authentication-and-permissions.html +++ b/tutorial/4-authentication-and-permissions.html @@ -311,10 +311,10 @@ url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()), <pre class="prettyprint lang-py"><code>from django.conf.urls import include </code></pre> <p>And, at the end of the file, add a pattern to include the login and logout views for the browsable API.</p> -<pre class="prettyprint lang-py"><code>urlpatterns += patterns('', +<pre class="prettyprint lang-py"><code>urlpatterns += [ url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), -) +] </code></pre> <p>The <code>r'^api-auth/'</code> part of pattern can actually be whatever URL you want to use. The only restriction is that the included urls must use the <code>'rest_framework'</code> namespace.</p> <p>Now if you open up the browser again and refresh the page you'll see a 'Login' link in the top right of the page. If you log in as one of the users you created earlier, you'll be able to create code snippets again.</p> diff --git a/tutorial/5-relationships-and-hyperlinked-apis.html b/tutorial/5-relationships-and-hyperlinked-apis.html index fccbfaaa..250a3d52 100644 --- a/tutorial/5-relationships-and-hyperlinked-apis.html +++ b/tutorial/5-relationships-and-hyperlinked-apis.html @@ -293,8 +293,8 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): </ul> <p>After adding all those names into our URLconf, our final <code>snippets/urls.py</code> file should look something like this:</p> <pre class="prettyprint lang-py"><code># API endpoints -urlpatterns = format_suffix_patterns(patterns('snippets.views', - url(r'^$', 'api_root'), +urlpatterns = format_suffix_patterns([ + url(r'^$', views.api_root), url(r'^snippets/$', views.SnippetList.as_view(), name='snippet-list'), @@ -310,13 +310,13 @@ urlpatterns = format_suffix_patterns(patterns('snippets.views', url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view(), name='user-detail') -)) +]) # Login and logout views for the browsable API -urlpatterns += patterns('', +urlpatterns += [ url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), -) +] </code></pre> <h2 id="adding-pagination">Adding pagination</h2> <p>The list views for users and code snippets could end up returning quite a lot of instances, so really we'd like to make sure we paginate the results, and allow the API client to step through each of the individual pages.</p> diff --git a/tutorial/6-viewsets-and-routers.html b/tutorial/6-viewsets-and-routers.html index f8d2f5eb..da4c6383 100644 --- a/tutorial/6-viewsets-and-routers.html +++ b/tutorial/6-viewsets-and-routers.html @@ -275,19 +275,19 @@ user_detail = UserViewSet.as_view({ </code></pre> <p>Notice how we're creating multiple views from each <code>ViewSet</code> class, by binding the http methods to the required action for each view.</p> <p>Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.</p> -<pre class="prettyprint lang-py"><code>urlpatterns = format_suffix_patterns(patterns('snippets.views', - url(r'^$', 'api_root'), +<pre class="prettyprint lang-py"><code>urlpatterns = format_suffix_patterns([ + url(r'^$', api_root), url(r'^snippets/$', snippet_list, name='snippet-list'), url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'), url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'), url(r'^users/$', user_list, name='user-list'), url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail') -)) +]) </code></pre> <h2 id="using-routers">Using Routers</h2> <p>Because we're using <code>ViewSet</code> classes rather than <code>View</code> classes, we actually don't need to design the URL conf ourselves. The conventions for wiring up resources into views and urls can be handled automatically, using a <code>Router</code> class. All we need to do is register the appropriate view sets with a router, and let it do the rest.</p> <p>Here's our re-wired <code>urls.py</code> file.</p> -<pre class="prettyprint lang-py"><code>from django.conf.urls import patterns, url, include +<pre class="prettyprint lang-py"><code>from django.conf.urls import url, include from snippets import views from rest_framework.routers import DefaultRouter @@ -298,10 +298,10 @@ router.register(r'users', views.UserViewSet) # The API URLs are now determined automatically by the router. # Additionally, we include the login URLs for the browseable API. -urlpatterns = patterns('', +urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) -) +] </code></pre> <p>Registering the viewsets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the viewset itself.</p> <p>The <code>DefaultRouter</code> class we're using also automatically creates the API root view for us, so we can now delete the <code>api_root</code> method from our <code>views</code> module.</p> |
