mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
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
This commit is contained in:
parent
fc6b192b70
commit
743fc247eb
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -103,11 +103,11 @@ 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()
|
||||
|
@ -115,10 +115,8 @@ Here's our re-wired `urls.py` file.
|
|||
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.
|
||||
|
|
|
@ -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 admin@example.com --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...
|
||||
|
||||
|
@ -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.
|
||||
|
||||
---
|
||||
|
|
Loading…
Reference in New Issue
Block a user