django-rest-framework/docs/index.rst

196 lines
6.9 KiB
ReStructuredText
Raw Normal View History

2011-02-01 22:55:57 +03:00
.. meta::
:description: A lightweight REST framework for Django.
:keywords: django, python, REST, RESTful
Django REST framework
=====================
2011-01-17 20:34:58 +03:00
2011-02-02 02:08:18 +03:00
Introduction
------------
2011-02-02 11:43:27 +03:00
Django REST framework is a lightweight REST framework for Django.
It aims to make it easy to build well-connected, self-describing Web APIs with a minimum of fuss.
2011-01-17 20:34:58 +03:00
Features:
2011-01-24 21:59:23 +03:00
* Clean, simple, class-based views for Resources.
2011-01-24 21:59:23 +03:00
* Support for ModelResources with nice default implementations and input validation.
* Automatically provides a browse-able self-documenting API.
* Pluggable Emitters, Parsers and Authenticators - Easy to customise.
* Content type negotiation using Accept headers.
* Optional support for forms as input validation.
* Modular architecture - Easy to extend and modify.
2011-01-30 19:51:06 +03:00
Requirements
------------
* Python 2.6
* Django 1.2
.. note::
Support for a wider range of Python & Django versions is planned, but right now django-rest-framework is only tested against these versions.
Installation & Setup
--------------------
The django-rest-framework project is hosted as a `mercurial repository on bitbucket <https://bitbucket.org/tomchristie/django-rest-framework>`_.
To get a local copy of the repository use mercurial::
2011-02-01 22:55:57 +03:00
hg clone https://bitbucket.org/tomchristie/django-rest-framework
To add django-rest-framework to a django project:
* Copy or symlink the ``djangorestframework`` directory into python's ``site-packages`` directory, or otherwise ensure that the ``djangorestframework`` directory is on your ``PYTHONPATH``.
* Add ``djangorestframework`` to your ``INSTALLED_APPS``.
* Ensure the ``TEMPLATE_LOADERS`` setting contains the item ``'django.template.loaders.app_directories.Loader'``. (It will do by default, so you shouldn't normally need to do anything here.)
2011-02-02 11:43:27 +03:00
Getting Started - Resources
---------------------------
2011-02-02 02:08:18 +03:00
We're going to start off with a simple example, that demonstrates
a few things:
2011-01-31 11:41:21 +03:00
2011-02-02 11:43:27 +03:00
#. Creating resources.
#. Linking resources.
#. Writing method handlers on resources.
#. Adding form validation to resources.
2011-02-02 02:08:18 +03:00
First we'll define two resources in our urlconf.
``urls.py``
2011-01-31 11:41:21 +03:00
2011-02-02 02:08:18 +03:00
.. include:: ../examples/resourceexample/urls.py
:literal:
Now we'll add a form that we'll use for input validation. This is completely optional, but it's often useful.
2011-01-31 11:41:21 +03:00
2011-02-02 02:08:18 +03:00
``forms.py``
.. include:: ../examples/resourceexample/forms.py
:literal:
2011-01-31 11:41:21 +03:00
2011-02-02 02:08:18 +03:00
Now we'll write our resources. The first is a read only resource that links to three instances of the second. The second resource just has some stub handler methods to help us see that our example is working.
``views.py``
.. include:: ../examples/resourceexample/views.py
:literal:
2011-02-02 11:44:47 +03:00
That's us done. Our API now provides both programmatic access using JSON and XML, as well a nice browseable HTML view, so we can now access it both from the browser:
2011-02-02 11:43:27 +03:00
* http://api.django-rest-framework.org/resource-example/
2011-02-02 11:44:47 +03:00
And from the command line:
2011-02-02 11:43:27 +03:00
.. code-block:: bash
2011-02-02 02:08:18 +03:00
2011-02-02 11:43:27 +03:00
# Demonstrates API's input validation using form input
bash: curl -X POST -H 'X-Requested-With: XMLHttpRequest' --data 'foo=true' http://api.django-rest-framework.org/resource-example/1/
{"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}}
# Demonstrates API's input validation using JSON input
bash: curl -X POST -H 'X-Requested-With: XMLHttpRequest' -H 'Content-Type: application/json' --data-binary '{"foo":true}' http://api.django-rest-framework.org/resource-example/1/
{"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}}
2011-02-02 02:08:18 +03:00
2011-02-02 11:43:27 +03:00
Getting Started - Model Resources
---------------------------------
2011-02-02 02:08:18 +03:00
2011-02-02 11:43:27 +03:00
Often you'll want parts of your API to directly map to existing django models. Django REST framework handles this nicely for you in a couple of ways:
2011-02-02 02:08:18 +03:00
2011-02-02 11:43:27 +03:00
#. It automatically provides suitable create/read/update/delete methods for your resources.
#. Input validation occurs automatically, by using appropriate `ModelForms <http://docs.djangoproject.com/en/dev/topics/forms/modelforms/>`_.
We'll start of defining two resources in our urlconf again.
``urls.py``
2011-02-02 02:08:18 +03:00
.. include:: ../examples/modelresourceexample/urls.py
:literal:
2011-02-02 11:43:27 +03:00
Here's the models we're working from in this example. It's usually a good idea to make sure you provide the :func:`get_absolute_url()` `permalink <http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url>`_ for all models you want to expose via the API.
2011-02-02 02:08:18 +03:00
``models.py``
.. include:: ../examples/modelresourceexample/models.py
:literal:
2011-02-05 00:41:53 +03:00
Now that we've got some models and a urlconf, there's very little code to write. We'll create a :class:`.ModelResource` to map to instances of our models, and a top level :class:`.RootModelResource` to list the existing instances and to create new instances.
2011-02-02 11:43:27 +03:00
``views.py``
2011-02-02 02:08:18 +03:00
.. include:: ../examples/modelresourceexample/views.py
:literal:
2011-01-31 11:41:21 +03:00
And we're done. We've now got a fully browseable API, which supports multiple input and output media types, and has all the nice automatic field validation that Django gives us for free.
We can visit the API in our browser:
2011-02-02 02:08:18 +03:00
* http://api.django-rest-framework.org/model-resource-example/
2011-01-31 11:41:21 +03:00
Or access it from the command line using curl:
.. code-block:: bash
2011-02-02 11:43:27 +03:00
# Demonstrates API's input validation using form input
bash: curl -X POST -H 'X-Requested-With: XMLHttpRequest' --data 'foo=true' http://api.django-rest-framework.org/model-resource-example/
2011-01-31 11:41:21 +03:00
{"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}}
2011-02-02 11:43:27 +03:00
# Demonstrates API's input validation using JSON input
bash: curl -X POST -H 'X-Requested-With: XMLHttpRequest' -H 'Content-Type: application/json' --data-binary '{"foo":true}' http://api.django-rest-framework.org/model-resource-example/
2011-01-31 11:48:26 +03:00
{"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}}
2011-02-02 11:43:27 +03:00
We could also have added the handler methods :meth:`.Resource.get()`, :meth:`.Resource.post()` etc... seen in the last example, but Django REST framework provides nice default implementations for us that do exactly what we'd expect them to.
Examples
--------
There's a few real world examples included with django-rest-framework.
These demonstrate the following use cases:
#. Using :class:`.Resource` for resources that do not map to models.
#. Using :class:`.Resource` with forms for input validation.
#. Using :class:`.ModelResource` for resources that map directly to models.
All the examples are freely available for testing in the sandbox here: http://api.django-rest-framework.org
.. toctree::
:maxdepth: 1
examples/objectstore
examples/pygments
examples/blogpost
2011-02-02 11:43:27 +03:00
How Tos, FAQs & Notes
---------------------
.. toctree::
:maxdepth: 2
howto/usingcurl
2011-02-02 11:43:27 +03:00
howto/alternativeframeworks
Library Reference
-----------------
.. toctree::
:maxdepth: 2
library/resource
library/modelresource
library/emitters
library/parsers
library/authenticators
library/response
2011-01-17 20:34:58 +03:00
Indices and tables
------------------
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`