mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Minor tutorial updates
This commit is contained in:
parent
aa40c58381
commit
a9218e460f
|
@ -18,7 +18,7 @@ Right now we have endpoints for 'snippets' and 'users', but we don't have a sing
|
||||||
'snippets': reverse('snippet-list', request=request, format=format)
|
'snippets': reverse('snippet-list', request=request, format=format)
|
||||||
})
|
})
|
||||||
|
|
||||||
Two things should be noticed here. First, we're using REST framework's `reverse` function in order to return fully-qualified URLs; second, URL patterns are identified by convenience names that we will declare later on in our `snippets/urls.py`.
|
Two things should be noticed here. First, we're using REST framework's `reverse` function in order to return fully-qualified URLs; second, URL patterns are identified by convenience names that we will declare later on in our `snippets/urls.py`.
|
||||||
|
|
||||||
## Creating an endpoint for the highlighted snippets
|
## Creating an endpoint for the highlighted snippets
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ We can easily re-write our existing serializers to use hyperlinking. In your `sn
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Snippet
|
model = Snippet
|
||||||
fields = ('url', 'highlight', 'owner',
|
fields = ('url', 'pk', 'highlight', 'owner',
|
||||||
'title', 'code', 'linenos', 'language', 'style')
|
'title', 'code', 'linenos', 'language', 'style')
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ We can easily re-write our existing serializers to use hyperlinking. In your `sn
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ('url', 'username', 'snippets')
|
fields = ('url', 'pk', 'username', 'snippets')
|
||||||
|
|
||||||
Notice that we've also added a new `'highlight'` field. This field is of the same type as the `url` field, except that it points to the `'snippet-highlight'` url pattern, instead of the `'snippet-detail'` url pattern.
|
Notice that we've also added a new `'highlight'` field. This field is of the same type as the `url` field, except that it points to the `'snippet-highlight'` url pattern, instead of the `'snippet-detail'` url pattern.
|
||||||
|
|
||||||
|
|
|
@ -67,9 +67,13 @@ also supported.
|
||||||
|
|
||||||
Now that our API is exposing a schema endpoint, we can use a dynamic client
|
Now that our API is exposing a schema endpoint, we can use a dynamic client
|
||||||
library to interact with the API. To demonstrate this, let's use the
|
library to interact with the API. To demonstrate this, let's use the
|
||||||
Core API command line client. We've already installed the `coreapi` package
|
Core API command line client.
|
||||||
using `pip`, so the client tool should already be installed. Check that it
|
|
||||||
is available on the command line...
|
The command line client is available as the `coreapi-cli` package:
|
||||||
|
|
||||||
|
$ pip install coreapi-cli
|
||||||
|
|
||||||
|
Now check that it is available on the command line...
|
||||||
|
|
||||||
$ coreapi
|
$ coreapi
|
||||||
Usage: coreapi [OPTIONS] COMMAND [ARGS]...
|
Usage: coreapi [OPTIONS] COMMAND [ARGS]...
|
||||||
|
@ -108,6 +112,7 @@ Let's try listing the existing snippets, using the command line client:
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"url": "http://127.0.0.1:8000/snippets/1/",
|
"url": "http://127.0.0.1:8000/snippets/1/",
|
||||||
|
"pk": 1,
|
||||||
"highlight": "http://127.0.0.1:8000/snippets/1/highlight/",
|
"highlight": "http://127.0.0.1:8000/snippets/1/highlight/",
|
||||||
"owner": "lucy",
|
"owner": "lucy",
|
||||||
"title": "Example",
|
"title": "Example",
|
||||||
|
@ -166,7 +171,7 @@ snippet:
|
||||||
$ coreapi action snippets create --param title "Example" --param code "print('hello, world')"
|
$ coreapi action snippets create --param title "Example" --param code "print('hello, world')"
|
||||||
{
|
{
|
||||||
"url": "http://127.0.0.1:8000/snippets/7/",
|
"url": "http://127.0.0.1:8000/snippets/7/",
|
||||||
"id": 7,
|
"pk": 7,
|
||||||
"highlight": "http://127.0.0.1:8000/snippets/7/highlight/",
|
"highlight": "http://127.0.0.1:8000/snippets/7/highlight/",
|
||||||
"owner": "lucy",
|
"owner": "lucy",
|
||||||
"title": "Example",
|
"title": "Example",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user