Skip to content

Commit 743fc24

Browse files
jpadillacarltongibson
authored andcommitted
Update tutorial (#5622)
* Use createsuperuser email and username flags * Only remove db.sqlite3 * Remove global permission class This interferes with Core API schema endpoint * Add default pagination class * Specify changes made in snippets/urls.py * Auth urls were already set in tutorial/urls.py * Specify changes made in snippets/urls.py * Use the suggested admin username from quickstart * Move global pagination setting away from quickstart section
1 parent fc6b192 commit 743fc24

6 files changed

+14
-28
lines changed

docs/tutorial/2-requests-and-responses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ and
106106

107107
def snippet_detail(request, pk, format=None):
108108

109-
Now update the `urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
109+
Now update the `snippets/urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
110110

111111
from django.conf.urls import url
112112
from rest_framework.urlpatterns import format_suffix_patterns

docs/tutorial/3-class-based-views.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
6262

6363
That's looking good. Again, it's still pretty similar to the function based view right now.
6464

65-
We'll also need to refactor our `urls.py` slightly now that we're using class-based views.
65+
We'll also need to refactor our `snippets/urls.py` slightly now that we're using class-based views.
6666

6767
from django.conf.urls import url
6868
from rest_framework.urlpatterns import format_suffix_patterns

docs/tutorial/4-authentication-and-permissions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ And now we can add a `.save()` method to our model class:
4343
When that's all done we'll need to update our database tables.
4444
Normally we'd create a database migration in order to do that, but for the purposes of this tutorial, let's just delete the database and start again.
4545

46-
rm -f tmp.db db.sqlite3
46+
rm -f db.sqlite3
4747
rm -r snippets/migrations
4848
python manage.py makemigrations snippets
4949
python manage.py migrate
@@ -205,11 +205,11 @@ If we try to create a snippet without authenticating, we'll get an error:
205205

206206
We can make a successful request by including the username and password of one of the users we created earlier.
207207

208-
http -a tom:password123 POST http://127.0.0.1:8000/snippets/ code="print 789"
208+
http -a admin:password123 POST http://127.0.0.1:8000/snippets/ code="print 789"
209209

210210
{
211211
"id": 1,
212-
"owner": "tom",
212+
"owner": "admin",
213213
"title": "foo",
214214
"code": "print 789",
215215
"linenos": false,

docs/tutorial/5-relationships-and-hyperlinked-apis.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,18 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
130130
name='user-detail')
131131
])
132132

133-
# Login and logout views for the browsable API
134-
urlpatterns += [
135-
url(r'^api-auth/', include('rest_framework.urls',
136-
namespace='rest_framework')),
137-
]
138-
139133
## Adding pagination
140134

141135
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.
142136

143-
We can change the default list style to use pagination, by modifying our `tutorial/settings.py` file slightly. Add the following setting:
137+
We can change the default list style to use pagination, by modifying our `tutorial/settings.py` file slightly. Add the following setting:
144138

145139
REST_FRAMEWORK = {
140+
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
146141
'PAGE_SIZE': 10
147142
}
148143

149-
Note that settings in REST framework are all namespaced into a single dictionary setting, named 'REST_FRAMEWORK', which helps keep them well separated from your other project settings.
144+
Note that settings in REST framework are all namespaced into a single dictionary setting, named `REST_FRAMEWORK`, which helps keep them well separated from your other project settings.
150145

151146
We could also customize the pagination style if we needed too, but in this case we'll just stick with the default.
152147

docs/tutorial/6-viewsets-and-routers.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The URLs for custom actions by default depend on the method name itself. If you
6161
The handler methods only get bound to the actions when we define the URLConf.
6262
To see what's going on under the hood let's first explicitly create a set of views from our ViewSets.
6363

64-
In the `urls.py` file we bind our `ViewSet` classes into a set of concrete views.
64+
In the `snippets/urls.py` file we bind our `ViewSet` classes into a set of concrete views.
6565

6666
from snippets.views import SnippetViewSet, UserViewSet, api_root
6767
from rest_framework import renderers
@@ -103,22 +103,20 @@ Now that we've bound our resources into concrete views, we can register the view
103103

104104
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.
105105

106-
Here's our re-wired `urls.py` file.
106+
Here's our re-wired `snippets/urls.py` file.
107107

108108
from django.conf.urls import url, include
109-
from snippets import views
110109
from rest_framework.routers import DefaultRouter
110+
from snippets import views
111111

112112
# Create a router and register our viewsets with it.
113113
router = DefaultRouter()
114114
router.register(r'snippets', views.SnippetViewSet)
115115
router.register(r'users', views.UserViewSet)
116116

117117
# The API URLs are now determined automatically by the router.
118-
# Additionally, we include the login URLs for the browsable API.
119118
urlpatterns = [
120-
url(r'^', include(router.urls)),
121-
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
119+
url(r'^', include(router.urls))
122120
]
123121

124122
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.

docs/tutorial/quickstart.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Now sync your database for the first time:
5454

5555
We'll also create an initial user named `admin` with a password of `password123`. We'll authenticate as that user later in our example.
5656

57-
python manage.py createsuperuser
57+
python manage.py createsuperuser --email [email protected] --username admin
5858

5959
Once you've set up a database and initial user created and ready to go, open up the app's directory and we'll get coding...
6060

@@ -134,20 +134,13 @@ Finally, we're including default login and logout views for use with the browsab
134134

135135
## Settings
136136

137-
We'd also like to set a few global settings. We'd like to turn on pagination, and we want our API to only be accessible to admin users. The settings module will be in `tutorial/settings.py`
137+
Add `'rest_framework'` to `INSTALLED_APPS`. The settings module will be in `tutorial/settings.py`
138138

139139
INSTALLED_APPS = (
140140
...
141141
'rest_framework',
142142
)
143143

144-
REST_FRAMEWORK = {
145-
'DEFAULT_PERMISSION_CLASSES': [
146-
'rest_framework.permissions.IsAdminUser',
147-
],
148-
'PAGE_SIZE': 10
149-
}
150-
151144
Okay, we're done.
152145

153146
---

0 commit comments

Comments
 (0)