mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
commit
287ff43cdd
|
@ -18,9 +18,9 @@
|
|||
|
||||
Django REST framework is a lightweight library that makes it easy to build Web APIs. It is designed as a modular and easy to customize architecture, based on Django's class based views.
|
||||
|
||||
Web APIs built using REST framework are fully self-describing and web browseable - a huge useability win for your developers. It also supports a wide range of media types, authentication and permission policies out of the box.
|
||||
Web APIs built using REST framework are fully self-describing and web browsable - a huge useability win for your developers. It also supports a wide range of media types, authentication and permission policies out of the box.
|
||||
|
||||
If you are considering using REST framework for your API, we recommend reading the [REST framework 2 announcment][rest-framework-2-announcement] which gives a good overview of the framework and it's capabilities.
|
||||
If you are considering using REST framework for your API, we recommend reading the [REST framework 2 announcement][rest-framework-2-announcement] which gives a good overview of the framework and it's capabilities.
|
||||
|
||||
There is also a sandbox API you can use for testing purposes, [available here][sandbox].
|
||||
|
||||
|
@ -41,7 +41,7 @@ There is also a sandbox API you can use for testing purposes, [available here][s
|
|||
Install using `pip`, including any optional packages you want...
|
||||
|
||||
pip install djangorestframework
|
||||
pip install markdown # Markdown support for the browseable API.
|
||||
pip install markdown # Markdown support for the browsable API.
|
||||
pip install pyyaml # YAML content-type support.
|
||||
pip install defusedxml # XML content-type support.
|
||||
pip install django-filter # Filtering support
|
||||
|
@ -60,7 +60,7 @@ Add `'rest_framework'` to your `INSTALLED_APPS` setting.
|
|||
'rest_framework',
|
||||
)
|
||||
|
||||
If you're intending to use the browseable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
||||
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
||||
|
||||
urlpatterns = patterns('',
|
||||
...
|
||||
|
|
|
@ -56,7 +56,7 @@ Or, if you're using the `@api_view` decorator with function based views.
|
|||
|
||||
It's important when specifying the renderer classes for your API to think about what priority you want to assign to each media type. If a client underspecifies the representations it can accept, such as sending an `Accept: */*` header, or not including an `Accept` header at all, then REST framework will select the first renderer in the list to use for the response.
|
||||
|
||||
For example if your API serves JSON responses and the HTML browseable API, you might want to make `JSONRenderer` your default renderer, in order to send `JSON` responses to clients that do not specify an `Accept` header.
|
||||
For example if your API serves JSON responses and the HTML browsable API, you might want to make `JSONRenderer` your default renderer, in order to send `JSON` responses to clients that do not specify an `Accept` header.
|
||||
|
||||
If your API includes views that can serve both regular webpages and API responses depending on the request, then you might consider making `TemplateHTMLRenderer` your default renderer, in order to play nicely with older browsers that send [broken accept headers][browser-accept-headers].
|
||||
|
||||
|
@ -166,7 +166,7 @@ See also: `TemplateHTMLRenderer`
|
|||
|
||||
## BrowsableAPIRenderer
|
||||
|
||||
Renders data into HTML for the Browseable API. This renderer will determine which other renderer would have been given highest priority, and use that to display an API style response within the HTML page.
|
||||
Renders data into HTML for the Browsable API. This renderer will determine which other renderer would have been given highest priority, and use that to display an API style response within the HTML page.
|
||||
|
||||
**.media_type**: `text/html`
|
||||
|
||||
|
|
5
docs/api-guide/serializers.md
Executable file → Normal file
5
docs/api-guide/serializers.md
Executable file → Normal file
|
@ -59,14 +59,15 @@ We can now use `CommentSerializer` to serialize a comment, or list of comments.
|
|||
|
||||
At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`.
|
||||
|
||||
stream = JSONRenderer().render(data)
|
||||
stream
|
||||
json = JSONRenderer().render(serializer.data)
|
||||
json
|
||||
# '{"email": "leila@example.com", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}'
|
||||
|
||||
## Deserializing objects
|
||||
|
||||
Deserialization is similar. First we parse a stream into python native datatypes...
|
||||
|
||||
stream = StringIO(json)
|
||||
data = JSONParser().parse(stream)
|
||||
|
||||
...then we restore those native datatypes into a fully populated object instance.
|
||||
|
|
|
@ -9,17 +9,17 @@
|
|||
|
||||
# Django REST framework
|
||||
|
||||
**Awesome web-browseable Web APIs.**
|
||||
**Awesome web-browsable Web APIs.**
|
||||
|
||||
Django REST framework is a flexible, powerful Web API toolkit. It is designed as a modular and easy to customize architecture, based on Django's class based views.
|
||||
|
||||
APIs built using REST framework are fully self-describing and web browseable - a huge useability win for your developers. It also supports a wide range of media types, authentication and permission policies out of the box.
|
||||
APIs built using REST framework are fully self-describing and web browsable - a huge useability win for your developers. It also supports a wide range of media types, authentication and permission policies out of the box.
|
||||
|
||||
If you are considering using REST framework for your API, we recommend reading the [REST framework 2 announcement][rest-framework-2-announcement] which gives a good overview of the framework and it's capabilities.
|
||||
|
||||
There is also a sandbox API you can use for testing purposes, [available here][sandbox].
|
||||
|
||||
**Below**: *Screenshot from the browseable API*
|
||||
**Below**: *Screenshot from the browsable API*
|
||||
|
||||
![Screenshot][image]
|
||||
|
||||
|
@ -32,7 +32,7 @@ REST framework requires the following:
|
|||
|
||||
The following packages are optional:
|
||||
|
||||
* [Markdown][markdown] (2.1.0+) - Markdown support for the browseable API.
|
||||
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
|
||||
* [PyYAML][yaml] (3.10+) - YAML content-type support.
|
||||
* [defusedxml][defusedxml] (0.3+) - XML content-type support.
|
||||
* [django-filter][django-filter] (0.5.4+) - Filtering support.
|
||||
|
@ -46,7 +46,7 @@ The following packages are optional:
|
|||
Install using `pip`, including any optional packages you want...
|
||||
|
||||
pip install djangorestframework
|
||||
pip install markdown # Markdown support for the browseable API.
|
||||
pip install markdown # Markdown support for the browsable API.
|
||||
pip install pyyaml # YAML content-type support.
|
||||
pip install django-filter # Filtering support
|
||||
|
||||
|
@ -64,7 +64,7 @@ Add `'rest_framework'` to your `INSTALLED_APPS` setting.
|
|||
'rest_framework',
|
||||
)
|
||||
|
||||
If you're intending to use the browseable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
||||
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
||||
|
||||
urlpatterns = patterns('',
|
||||
...
|
||||
|
|
|
@ -57,7 +57,7 @@ You can determine your currently installed version using `pip freeze`:
|
|||
**Date**: 4th April 2013
|
||||
|
||||
* OAuth2 authentication no longer requires unneccessary URL parameters in addition to the token.
|
||||
* URL hyperlinking in browseable API now handles more cases correctly.
|
||||
* URL hyperlinking in browsable API now handles more cases correctly.
|
||||
* Long HTTP headers in browsable API are broken in multiple lines when possible.
|
||||
* Bugfix: Fix regression with DjangoFilterBackend not worthing correctly with single object views.
|
||||
* Bugfix: OAuth should fail hard when invalid token used.
|
||||
|
@ -107,10 +107,10 @@ You can determine your currently installed version using `pip freeze`:
|
|||
**Date**: 22nd Feb 2013
|
||||
|
||||
* Security fix: Use `defusedxml` package to address XML parsing vulnerabilities.
|
||||
* Raw data tab added to browseable API. (Eg. Allow for JSON input.)
|
||||
* Raw data tab added to browsable API. (Eg. Allow for JSON input.)
|
||||
* Added TimeField.
|
||||
* Serializer fields can be mapped to any method that takes no args, or only takes kwargs which have defaults.
|
||||
* Unicode support for view names/descriptions in browseable API.
|
||||
* Unicode support for view names/descriptions in browsable API.
|
||||
* Bugfix: request.DATA should return an empty `QueryDict` with no data, not `None`.
|
||||
* Bugfix: Remove unneeded field validation, which caused extra queries.
|
||||
|
||||
|
@ -207,14 +207,14 @@ This change will not affect user code, so long as it's following the recommended
|
|||
**Date**: 21st Dec 2012
|
||||
|
||||
* Bugfix: Fix bug that could occur using ChoiceField.
|
||||
* Bugfix: Fix exception in browseable API on DELETE.
|
||||
* Bugfix: Fix exception in browsable API on DELETE.
|
||||
* Bugfix: Fix issue where pk was was being set to a string if set by URL kwarg.
|
||||
|
||||
### 2.1.11
|
||||
|
||||
**Date**: 17th Dec 2012
|
||||
|
||||
* Bugfix: Fix issue with M2M fields in browseable API.
|
||||
* Bugfix: Fix issue with M2M fields in browsable API.
|
||||
|
||||
### 2.1.10
|
||||
|
||||
|
@ -310,7 +310,7 @@ This change will not affect user code, so long as it's following the recommended
|
|||
* Hyperlinked related fields optionally take `slug_field` and `slug_url_kwarg` arguments.
|
||||
* Support Django's cache framework.
|
||||
* Minor field improvements. (Don't stringify dicts, more robust many-pk fields.)
|
||||
* Bugfix: Support choice field in Browseable API.
|
||||
* Bugfix: Support choice field in Browsable API.
|
||||
* Bugfix: Related fields with `read_only=True` do not require a `queryset` argument.
|
||||
|
||||
**API-incompatible changes**: Please read [this thread][2.1.0-notes] regarding the `instance` and `data` keyword args before updating to 2.1.0.
|
||||
|
|
|
@ -62,19 +62,19 @@ REST framework 2 also allows you to work with both function-based and class-base
|
|||
|
||||
Pretty much every aspect of REST framework has been reworked, with the aim of ironing out some of the design flaws of the previous versions. Each of the components of REST framework are cleanly decoupled, and can be used independantly of each-other, and there are no monolithic resource classes, overcomplicated mixin combinations, or opinionated serialization or URL routing decisions.
|
||||
|
||||
## The Browseable API
|
||||
## The Browsable API
|
||||
|
||||
Django REST framework's most unique feature is the way it is able to serve up both machine-readable representations, and a fully browsable HTML representation to the same endpoints.
|
||||
|
||||
Browseable Web APIs are easier to work with, visualize and debug, and generally makes it easier and more frictionless to inspect and work with.
|
||||
Browsable Web APIs are easier to work with, visualize and debug, and generally makes it easier and more frictionless to inspect and work with.
|
||||
|
||||
With REST framework 2, the browseable API gets a snazzy new bootstrap-based theme that looks great and is even nicer to work with.
|
||||
With REST framework 2, the browsable API gets a snazzy new bootstrap-based theme that looks great and is even nicer to work with.
|
||||
|
||||
There are also some functionality improvments - actions such as as `POST` and `DELETE` will only display if the user has the appropriate permissions.
|
||||
|
||||
![Browseable API][image]
|
||||
![Browsable API][image]
|
||||
|
||||
**Image above**: An example of the browseable API in REST framework 2
|
||||
**Image above**: An example of the browsable API in REST framework 2
|
||||
|
||||
## Documentation
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ REST framework is an agnostic Web API toolkit. It does help guide you towards b
|
|||
|
||||
## What REST framework provides.
|
||||
|
||||
It is self evident that REST framework makes it possible to build Hypermedia APIs. The browseable API that it offers is built on HTML - the hypermedia language of the web.
|
||||
It is self evident that REST framework makes it possible to build Hypermedia APIs. The browsable API that it offers is built on HTML - the hypermedia language of the web.
|
||||
|
||||
REST framework also includes [serialization] and [parser]/[renderer] components that make it easy to build appropriate media types, [hyperlinked relations][fields] for building well-connected systems, and great support for [content negotiation][conneg].
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ We can control the format of the response that we get back, either by using the
|
|||
Or by appending a format suffix:
|
||||
|
||||
curl http://127.0.0.1:8000/snippets/.json # JSON suffix
|
||||
curl http://127.0.0.1:8000/snippets/.api # Browseable API suffix
|
||||
curl http://127.0.0.1:8000/snippets/.api # Browsable API suffix
|
||||
|
||||
Similarly, we can control the format of the request that we send, using the `Content-Type` header.
|
||||
|
||||
|
@ -160,9 +160,9 @@ Now go and open the API in a web browser, by visiting [http://127.0.0.1:8000/sni
|
|||
|
||||
Because the API chooses the content type of the response based on the client request, it will, by default, return an HTML-formatted representation of the resource when that resource is requested by a web browser. This allows for the API to return a fully web-browsable HTML representation.
|
||||
|
||||
Having a web-browseable API is a huge usability win, and makes developing and using your API much easier. It also dramatically lowers the barrier-to-entry for other developers wanting to inspect and work with your API.
|
||||
Having a web-browsable API is a huge usability win, and makes developing and using your API much easier. It also dramatically lowers the barrier-to-entry for other developers wanting to inspect and work with your API.
|
||||
|
||||
See the [browsable api][browseable-api] topic for more information about the browsable API feature and how to customize it.
|
||||
See the [browsable api][browsable-api] topic for more information about the browsable API feature and how to customize it.
|
||||
|
||||
## What's next?
|
||||
|
||||
|
@ -170,6 +170,6 @@ In [tutorial part 3][tut-3], we'll start using class based views, and see how ge
|
|||
|
||||
[json-url]: http://example.com/api/items/4.json
|
||||
[devserver]: http://127.0.0.1:8000/snippets/
|
||||
[browseable-api]: ../topics/browsable-api.md
|
||||
[browsable-api]: ../topics/browsable-api.md
|
||||
[tut-1]: 1-serialization.md
|
||||
[tut-3]: 3-class-based-views.md
|
||||
|
|
|
@ -118,17 +118,17 @@ Then, add the following property to **both** the `SnippetList` and `SnippetDetai
|
|||
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
## Adding login to the Browseable API
|
||||
## Adding login to the Browsable API
|
||||
|
||||
If you open a browser and navigate to the browseable API at the moment, you'll find that you're no longer able to create new code snippets. In order to do so we'd need to be able to login as a user.
|
||||
If you open a browser and navigate to the browsable API at the moment, you'll find that you're no longer able to create new code snippets. In order to do so we'd need to be able to login as a user.
|
||||
|
||||
We can add a login view for use with the browseable API, by editing our URLconf once more.
|
||||
We can add a login view for use with the browsable API, by editing our URLconf once more.
|
||||
|
||||
Add the following import at the top of the file:
|
||||
|
||||
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 browseable API.
|
||||
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
|
||||
|
||||
urlpatterns += patterns('',
|
||||
url(r'^api-auth/', include('rest_framework.urls',
|
||||
|
|
|
@ -149,11 +149,11 @@ We could also customize the pagination style if we needed too, but in this case
|
|||
|
||||
## Reviewing our work
|
||||
|
||||
If we open a browser and navigate to the browseable API, you'll find that you can now work your way around the API simply by following links.
|
||||
If we open a browser and navigate to the browsable API, you'll find that you can now work your way around the API simply by following links.
|
||||
|
||||
You'll also be able to see the 'highlight' links on the snippet instances, that will take you to the highlighted code HTML representations.
|
||||
|
||||
We've now got a complete pastebin Web API, which is fully web browseable, and comes complete with authentication, per-object permissions, and multiple renderer formats.
|
||||
We've now got a complete pastebin Web API, which is fully web browsable, and comes complete with authentication, per-object permissions, and multiple renderer formats.
|
||||
|
||||
We've walked through each step of the design process, and seen how if we need to customize anything we can gradually work our way down to simply using regular Django views.
|
||||
|
||||
|
|
|
@ -121,7 +121,7 @@ Firstly the names `user-detail` and `group-detail` are important. We're using t
|
|||
|
||||
Secondly, we're modifying the urlpatterns using `format_suffix_patterns`, to append optional `.json` style suffixes to our URLs.
|
||||
|
||||
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browseable API.
|
||||
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
|
||||
|
||||
## Settings
|
||||
|
||||
|
|
|
@ -133,7 +133,7 @@ for (dirpath, dirnames, filenames) in os.walk(docs_dir):
|
|||
toc += template + '\n'
|
||||
|
||||
if filename == 'index.md':
|
||||
main_title = 'Django REST framework - Web Browseable APIs'
|
||||
main_title = 'Django REST framework - Web Browsable APIs'
|
||||
else:
|
||||
main_title = 'Django REST framework - ' + main_title
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ class JSONRenderer(BaseRenderer):
|
|||
return ''
|
||||
|
||||
# If 'indent' is provided in the context, then pretty print the result.
|
||||
# E.g. If we're being called by the BrowseableAPIRenderer.
|
||||
# E.g. If we're being called by the BrowsableAPIRenderer.
|
||||
renderer_context = renderer_context or {}
|
||||
indent = renderer_context.get('indent', None)
|
||||
|
||||
|
|
|
@ -377,7 +377,7 @@ class TestCreateModelWithAutoNowAddField(TestCase):
|
|||
self.assertEqual(created.content, 'foobar')
|
||||
|
||||
|
||||
# Test for particularly ugly regression with m2m in browseable API
|
||||
# Test for particularly ugly regression with m2m in browsable API
|
||||
class ClassB(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
|
@ -402,7 +402,7 @@ class ExampleView(generics.ListCreateAPIView):
|
|||
class TestM2MBrowseableAPI(TestCase):
|
||||
def test_m2m_in_browseable_api(self):
|
||||
"""
|
||||
Test for particularly ugly regression with m2m in browseable API
|
||||
Test for particularly ugly regression with m2m in browsable API
|
||||
"""
|
||||
request = factory.get('/', HTTP_ACCEPT='text/html')
|
||||
view = ExampleView().as_view()
|
||||
|
|
Loading…
Reference in New Issue
Block a user