From dd14c6c88ba210bac8349041b8db486576539249 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Nov 2014 11:21:29 +0000 Subject: Latest docs release --- tutorial/1-serialization.html | 13 +++++++------ tutorial/2-requests-and-responses.html | 9 +++++---- tutorial/3-class-based-views.html | 4 ++-- tutorial/4-authentication-and-permissions.html | 4 ++-- tutorial/5-relationships-and-hyperlinked-apis.html | 10 +++++----- tutorial/6-viewsets-and-routers.html | 12 ++++++------ 6 files changed, 27 insertions(+), 25 deletions(-) (limited to 'tutorial') 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 )
We also need to wire up the root urlconf, in the tutorial/urls.py file, to include our snippet app's URLs.
urlpatterns = patterns('',
+urlpatterns = [
url(r'^', include('snippets.urls')),
-)
+]
Okay, we're ready to roll.
Creating a model to work with
@@ -459,11 +459,12 @@ def snippet_detail(request, pk):
Finally we need to wire these views up. Create the snippets/urls.py file:
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),
+]
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 json, 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.
Now update the urls.py file slightly, to append a set of format_suffix_patterns in addition to the existing URLs.
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)
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)
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()),
from django.conf.urls import include
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
-urlpatterns += patterns('',
+urlpatterns += [
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
-)
+]
The r'^api-auth/' part of pattern can actually be whatever URL you want to use. The only restriction is that the included urls must use the 'rest_framework' namespace.
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.
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):
After adding all those names into our URLconf, our final snippets/urls.py file should look something like this:
# 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')),
-)
+]
Adding pagination
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.
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({
Notice how we're creating multiple views from each ViewSet class, by binding the http methods to the required action for each view.
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
-urlpatterns = format_suffix_patterns(patterns('snippets.views',
- url(r'^$', 'api_root'),
+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')
-))
+])
Using Routers
Because we're using ViewSet classes rather than View 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 Router class. All we need to do is register the appropriate view sets with a router, and let it do the rest.
Here's our re-wired urls.py file.
-from django.conf.urls import patterns, url, include
+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'))
-)
+]
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.
The DefaultRouter class we're using also automatically creates the API root view for us, so we can now delete the api_root method from our views module.
--
cgit v1.2.3