Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tutorial #5622

Merged
merged 9 commits into from
Nov 27, 2017
2 changes: 1 addition & 1 deletion docs/tutorial/2-requests-and-responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ and

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

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

from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/3-class-based-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be

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

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

from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial/4-authentication-and-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ And now we can add a `.save()` method to our model class:
When that's all done we'll need to update our database tables.
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.

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

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

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

{
"id": 1,
"owner": "tom",
"owner": "admin",
"title": "foo",
"code": "print 789",
"linenos": false,
Expand Down
11 changes: 3 additions & 8 deletions docs/tutorial/5-relationships-and-hyperlinked-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,18 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
name='user-detail')
])

# Login and logout views for the browsable API
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.

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

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}

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

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

Expand Down
10 changes: 4 additions & 6 deletions docs/tutorial/6-viewsets-and-routers.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The URLs for custom actions by default depend on the method name itself. If you
The handler methods only get bound to the actions when we define the URLConf.
To see what's going on under the hood let's first explicitly create a set of views from our ViewSets.

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

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

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.
Here's our re-wired `snippets/urls.py` file.

from django.conf.urls import url, include
from snippets import views
from rest_framework.routers import DefaultRouter
from snippets import views

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

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

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.
Expand Down
11 changes: 2 additions & 9 deletions docs/tutorial/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Now sync your database for the first time:

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

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

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

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

## Settings

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`
Add `'rest_framework'` to `INSTALLED_APPS`. The settings module will be in `tutorial/settings.py`

INSTALLED_APPS = (
...
'rest_framework',
)

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAdminUser',
],
'PAGE_SIZE': 10
}

Okay, we're done.

---
Expand Down