2013-01-22 21:35:46 +04:00
< p class = "badges" >
< iframe src = "http://ghbtns.com/github-btn.html?user=tomchristie&repo=django-rest-framework&type=watch&count=true" class = "github-star-button" allowtransparency = "true" frameborder = "0" scrolling = "0" width = "110px" height = "20px" > < / iframe >
2013-06-04 23:23:16 +04:00
< a href = "https://twitter.com/share" class = "twitter-share-button" data-url = "django-rest-framework.org" data-text = "Checking out the totally awesome Django REST framework! http://django-rest-framework.org" data-count = "none" > < / a >
2013-01-22 21:35:46 +04:00
< script > ! function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] ; if ( ! d . getElementById ( id ) ) { js = d . createElement ( s ) ; js . id = id ; js . src = "http://platform.twitter.com/widgets.js" ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , "script" , "twitter-wjs" ) ; < / script >
2013-06-04 23:23:16 +04:00
< img src = "https://secure.travis-ci.org/tomchristie/django-rest-framework.png?branch=master" class = "travis-build-image" >
2013-01-22 21:35:46 +04:00
< / p >
2012-09-09 19:56:04 +04:00
2012-09-01 23:26:27 +04:00
# Django REST framework
2013-05-02 08:26:40 +04:00
**Awesome web-browsable Web APIs.**
2012-09-01 23:26:27 +04:00
2013-04-30 12:38:04 +04:00
Django REST framework is a powerful and flexible toolkit that makes it easy to build Web APIs.
2012-09-01 23:26:27 +04:00
2013-04-30 12:38:04 +04:00
Some reasons you might want to use REST framework:
2012-09-01 23:26:27 +04:00
2013-05-28 19:13:12 +04:00
* The Web browseable API is a huge usability win for your developers.
2013-04-30 12:38:04 +04:00
* Authentication policies including OAuth1a and OAuth2 out of the box.
* Serialization that supports both ORM and non-ORM data sources.
2013-04-30 17:34:42 +04:00
* Customizable all the way down - just use regular function-based views if you don't need the more powerful features.
2013-04-30 12:38:04 +04:00
* Extensive documentation, and great community support.
2012-10-30 16:23:17 +04:00
2013-04-30 12:38:04 +04:00
There is a live example API for testing purposes, [available here][sandbox].
2012-10-30 16:23:17 +04:00
2013-05-02 08:26:40 +04:00
**Below**: *Screenshot from the browsable API*
2012-10-30 16:23:17 +04:00
![Screenshot][image]
2012-09-01 23:26:27 +04:00
## Requirements
REST framework requires the following:
2013-02-15 13:05:55 +04:00
* Python (2.6.5+, 2.7, 3.2, 3.3)
2013-06-03 00:14:02 +04:00
* Django (1.3, 1.4, 1.5, 1.6)
2012-09-01 23:26:27 +04:00
The following packages are optional:
2013-05-02 08:26:40 +04:00
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
2012-09-03 15:11:57 +04:00
* [PyYAML][yaml] (3.10+) - YAML content-type support.
2013-02-22 17:17:22 +04:00
* [defusedxml][defusedxml] (0.3+) - XML content-type support.
2012-11-17 00:58:49 +04:00
* [django-filter][django-filter] (0.5.4+) - Filtering support.
2013-03-07 13:01:53 +04:00
* [django-oauth-plus][django-oauth-plus] (2.0+) and [oauth2][oauth2] (1.5.211+) - OAuth 1.0a support.
2013-03-07 21:50:48 +04:00
* [django-oauth2-provider][django-oauth2-provider] (0.2.3+) - OAuth 2.0 support.
2013-03-07 13:01:53 +04:00
2013-03-07 21:50:48 +04:00
**Note**: The `oauth2` python package is badly misnamed, and actually provides OAuth 1.0a support. Also note that packages required for both OAuth 1.0a, and OAuth 2.0 are not yet Python 3 compatible.
2012-09-01 23:26:27 +04:00
## Installation
2012-09-03 15:11:57 +04:00
Install using `pip` , including any optional packages you want...
2012-09-01 23:26:27 +04:00
pip install djangorestframework
2013-05-05 21:12:35 +04:00
pip install markdown # Markdown support for the browsable API.
2012-11-17 00:58:49 +04:00
pip install django-filter # Filtering support
2012-09-01 23:26:27 +04:00
...or clone the project from github.
git clone git@github.com:tomchristie/django-rest-framework.git
2012-12-27 21:53:07 +04:00
Add `'rest_framework'` to your `INSTALLED_APPS` setting.
2012-09-01 23:26:27 +04:00
INSTALLED_APPS = (
...
2012-09-20 16:06:27 +04:00
'rest_framework',
2012-09-01 23:26:27 +04:00
)
2013-05-02 08:26:40 +04:00
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.
2012-09-01 23:26:27 +04:00
urlpatterns = patterns('',
...
2012-09-20 16:06:27 +04:00
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
2012-09-01 23:26:27 +04:00
)
2012-09-20 16:06:27 +04:00
2012-12-27 21:53:07 +04:00
Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace.
2012-09-20 16:06:27 +04:00
2013-04-25 01:40:24 +04:00
## Example
Let's take a look at a quick example of using REST framework to build a simple model-backed API.
We'll create a read-write API for accessing users and groups.
2013-04-30 17:34:42 +04:00
Any global settings for a REST framework API are kept in a single configuration dictionary named `REST_FRAMEWORK` . Start off by adding the following to your `settings.py` module:
REST_FRAMEWORK = {
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
Don't forget to make sure you've also added `rest_framework` to your `INSTALLED_APPS` .
We're ready to create our API now.
2013-04-30 11:24:33 +04:00
Here's our project's root `urls.py` module:
2013-04-29 16:20:15 +04:00
2013-04-25 01:40:24 +04:00
from django.conf.urls.defaults import url, patterns, include
from django.contrib.auth.models import User, Group
2013-04-29 16:20:15 +04:00
from rest_framework import viewsets, routers
2013-04-25 01:40:24 +04:00
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
2013-04-30 11:24:33 +04:00
model = User
2013-04-25 01:40:24 +04:00
class GroupViewSet(viewsets.ModelViewSet):
2013-04-30 11:24:33 +04:00
model = Group
2013-04-25 01:40:24 +04:00
2013-04-30 11:24:33 +04:00
# Routers provide an easy way of automatically determining the URL conf
2013-04-29 16:20:15 +04:00
router = routers.DefaultRouter()
2013-05-16 11:47:22 +04:00
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
2013-04-29 16:20:15 +04:00
2013-04-30 11:24:33 +04:00
2013-04-29 16:20:15 +04:00
# Wire up our API using automatic URL routing.
2013-04-25 01:40:24 +04:00
# Additionally, we include login URLs for the browseable API.
urlpatterns = patterns('',
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
2012-09-01 23:26:27 +04:00
## Quickstart
2012-08-29 23:57:37 +04:00
2013-03-14 00:53:39 +04:00
Can't wait to get started? The [quickstart guide][quickstart] is the fastest way to get up and running, and building APIs with REST framework.
2012-10-05 13:23:47 +04:00
2012-09-01 23:26:27 +04:00
## Tutorial
The tutorial will walk you through the building blocks that make up REST framework. It'll take a little while to get through, but it'll give you a comprehensive understanding of how everything fits together, and is highly recommended reading.
2012-08-29 23:57:37 +04:00
* [1 - Serialization][tut-1]
* [2 - Requests & Responses][tut-2]
* [3 - Class based views][tut-3]
2012-10-28 23:37:27 +04:00
* [4 - Authentication & permissions][tut-4]
2012-08-29 23:57:37 +04:00
* [5 - Relationships & hyperlinked APIs][tut-5]
2013-04-09 14:54:51 +04:00
* [6 - Viewsets & routers][tut-6]
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
## API Guide
The API guide is your complete reference manual to all the functionality provided by REST framework.
2012-08-29 23:57:37 +04:00
* [Requests][request]
* [Responses][response]
* [Views][views]
2012-09-12 13:12:13 +04:00
* [Generic views][generic-views]
2013-04-09 14:54:51 +04:00
* [Viewsets][viewsets]
* [Routers][routers]
2012-08-29 23:57:37 +04:00
* [Parsers][parsers]
* [Renderers][renderers]
* [Serializers][serializers]
2012-10-05 20:10:07 +04:00
* [Serializer fields][fields]
2012-12-31 12:53:40 +04:00
* [Serializer relations][relations]
2012-08-29 23:57:37 +04:00
* [Authentication][authentication]
* [Permissions][permissions]
2012-09-05 13:01:43 +04:00
* [Throttling][throttling]
2012-11-08 01:28:10 +04:00
* [Filtering][filtering]
2012-10-01 19:17:01 +04:00
* [Pagination][pagination]
2012-09-12 13:12:13 +04:00
* [Content negotiation][contentnegotiation]
* [Format suffixes][formatsuffixes]
* [Returning URLs][reverse]
2012-09-01 23:26:27 +04:00
* [Exceptions][exceptions]
2012-08-29 23:57:37 +04:00
* [Status codes][status]
2012-09-05 13:01:43 +04:00
* [Settings][settings]
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
## Topics
General guides to using REST framework.
2012-08-29 23:57:37 +04:00
2013-01-27 21:23:56 +04:00
* [AJAX, CSRF & CORS][ajax-csrf-cors]
2012-10-13 18:07:43 +04:00
* [Browser enhancements][browser-enhancements]
2012-10-13 18:34:38 +04:00
* [The Browsable API][browsableapi]
2012-10-08 15:17:43 +04:00
* [REST, Hypermedia & HATEOAS][rest-hypermedia-hateoas]
2012-10-27 21:39:17 +04:00
* [2.0 Announcement][rest-framework-2-announcement]
2013-02-13 18:08:56 +04:00
* [2.2 Announcement][2.2-announcement]
2013-04-25 23:43:37 +04:00
* [2.3 Announcement][2.3-announcement]
2012-10-17 16:51:15 +04:00
* [Release Notes][release-notes]
2012-09-02 00:23:50 +04:00
* [Credits][credits]
2012-08-29 23:57:37 +04:00
2012-09-02 00:35:30 +04:00
## Development
If you want to work on REST framework itself, clone the repository, then...
Build the docs:
./mkdocs.py
Run the tests:
2012-09-20 16:06:27 +04:00
./rest_framework/runtests/runtests.py
2012-09-02 00:35:30 +04:00
2013-03-06 16:19:49 +04:00
To run the tests against all supported configurations, first install [the tox testing tool][tox] globally, using `pip install tox` , then simply run `tox` :
tox
2012-10-09 15:01:17 +04:00
## Support
2013-01-18 18:04:26 +04:00
For support please see the [REST framework discussion group][group], try the `#restframework` channel on `irc.freenode.net` , or raise a question on [Stack Overflow][stack-overflow], making sure to include the ['django-rest-framework'][django-rest-framework-tag] tag.
2012-10-09 15:01:17 +04:00
2013-01-18 18:04:26 +04:00
[Paid support is available][paid-support] from [DabApps][dabapps], and can include work on REST framework core, or support with building your REST framework API. Please [contact DabApps][contact-dabapps] if you'd like to discuss commercial support options.
2012-10-09 15:01:17 +04:00
2013-01-24 17:01:42 +04:00
For updates on REST framework development, you may also want to follow [the author][twitter] on Twitter.
< a style = "padding-top: 10px" href = "https://twitter.com/_tomchristie" class = "twitter-follow-button" data-show-count = "false" > Follow @_tomchristie </ a >
< script > ! function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] ; if ( ! d . getElementById ( id ) ) { js = d . createElement ( s ) ; js . id = id ; js . src = "//platform.twitter.com/widgets.js" ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , "script" , "twitter-wjs" ) ; < / script >
2013-05-22 19:45:33 +04:00
## Security
If you believe you’ ve found something in Django REST framework which has security implications, please **do not raise the issue in a public forum** .
Send a description of the issue via email to [rest-framework-security@googlegroups.com][security-mail]. The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.
2012-09-01 23:26:27 +04:00
## License
2013-01-04 01:43:54 +04:00
Copyright (c) 2011-2013, Tom Christie
2012-09-01 23:26:27 +04:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2012-10-30 18:48:21 +04:00
[travis]: http://travis-ci.org/tomchristie/django-rest-framework?branch=master
2013-01-04 01:43:54 +04:00
[travis-build-image]: https://secure.travis-ci.org/tomchristie/django-rest-framework.png?branch=master
2012-09-01 23:26:27 +04:00
[urlobject]: https://github.com/zacharyvoase/urlobject
[markdown]: http://pypi.python.org/pypi/Markdown/
[yaml]: http://pypi.python.org/pypi/PyYAML
2013-02-22 17:17:22 +04:00
[defusedxml]: https://pypi.python.org/pypi/defusedxml
2013-01-12 17:31:40 +04:00
[django-filter]: http://pypi.python.org/pypi/django-filter
2013-03-07 13:01:53 +04:00
[oauth2]: https://github.com/simplegeo/python-oauth2
[django-oauth-plus]: https://bitbucket.org/david/django-oauth-plus/wiki/Home
2013-03-07 21:50:48 +04:00
[django-oauth2-provider]: https://github.com/caffeinehit/django-oauth2-provider
2012-10-30 16:23:17 +04:00
[0.4]: https://github.com/tomchristie/django-rest-framework/tree/0.4.X
[image]: img/quickstart.png
[sandbox]: http://restframework.herokuapp.com/
2012-08-29 23:57:37 +04:00
2012-10-09 15:01:17 +04:00
[quickstart]: tutorial/quickstart.md
2012-08-29 23:57:37 +04:00
[tut-1]: tutorial/1-serialization.md
[tut-2]: tutorial/2-requests-and-responses.md
[tut-3]: tutorial/3-class-based-views.md
2012-10-28 23:37:27 +04:00
[tut-4]: tutorial/4-authentication-and-permissions.md
2012-08-29 23:57:37 +04:00
[tut-5]: tutorial/5-relationships-and-hyperlinked-apis.md
2013-03-31 14:36:58 +04:00
[tut-6]: tutorial/6-viewsets-and-routers.md
2012-08-29 23:57:37 +04:00
2012-09-01 23:26:27 +04:00
[request]: api-guide/requests.md
[response]: api-guide/responses.md
[views]: api-guide/views.md
2012-09-12 13:12:13 +04:00
[generic-views]: api-guide/generic-views.md
2013-04-09 14:54:51 +04:00
[viewsets]: api-guide/viewsets.md
[routers]: api-guide/routers.md
2012-09-01 23:26:27 +04:00
[parsers]: api-guide/parsers.md
[renderers]: api-guide/renderers.md
[serializers]: api-guide/serializers.md
2012-10-05 20:10:07 +04:00
[fields]: api-guide/fields.md
2012-12-31 12:53:40 +04:00
[relations]: api-guide/relations.md
2012-09-01 23:26:27 +04:00
[authentication]: api-guide/authentication.md
[permissions]: api-guide/permissions.md
2012-09-05 13:01:43 +04:00
[throttling]: api-guide/throttling.md
2012-11-08 01:28:10 +04:00
[filtering]: api-guide/filtering.md
2012-10-01 19:17:01 +04:00
[pagination]: api-guide/pagination.md
2012-09-12 13:12:13 +04:00
[contentnegotiation]: api-guide/content-negotiation.md
[formatsuffixes]: api-guide/format-suffixes.md
[reverse]: api-guide/reverse.md
2012-09-01 23:26:27 +04:00
[exceptions]: api-guide/exceptions.md
2012-09-05 16:03:55 +04:00
[status]: api-guide/status-codes.md
2012-09-05 13:01:43 +04:00
[settings]: api-guide/settings.md
2012-09-01 23:26:27 +04:00
2013-01-27 21:23:56 +04:00
[ajax-csrf-cors]: topics/ajax-csrf-cors.md
2012-10-13 18:07:43 +04:00
[browser-enhancements]: topics/browser-enhancements.md
2012-09-10 00:46:05 +04:00
[browsableapi]: topics/browsable-api.md
2012-10-08 15:17:43 +04:00
[rest-hypermedia-hateoas]: topics/rest-hypermedia-hateoas.md
2012-09-05 16:03:55 +04:00
[contributing]: topics/contributing.md
2012-10-27 21:39:17 +04:00
[rest-framework-2-announcement]: topics/rest-framework-2-announcement.md
2013-02-13 18:08:56 +04:00
[2.2-announcement]: topics/2.2-announcement.md
2013-04-25 23:43:37 +04:00
[2.3-announcement]: topics/2.3-announcement.md
2012-10-17 16:51:15 +04:00
[release-notes]: topics/release-notes.md
2012-09-02 00:23:50 +04:00
[credits]: topics/credits.md
2012-10-09 15:01:17 +04:00
2013-03-06 16:19:49 +04:00
[tox]: http://testrun.org/tox/latest/
2012-10-09 15:01:17 +04:00
[group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
2013-01-18 18:04:26 +04:00
[stack-overflow]: http://stackoverflow.com/
[django-rest-framework-tag]: http://stackoverflow.com/questions/tagged/django-rest-framework
[django-tag]: http://stackoverflow.com/questions/tagged/django
2013-05-22 19:45:33 +04:00
[security-mail]: mailto:rest-framework-security@googlegroups.com
2013-01-18 18:04:26 +04:00
[paid-support]: http://dabapps.com/services/build/api-development/
[dabapps]: http://dabapps.com
[contact-dabapps]: http://dabapps.com/contact/
2013-01-24 17:01:42 +04:00
[twitter]: https://twitter.com/_tomchristie