Django REST framework API

转自:https://django-rest-framework.readthedocs.org/en/0.3.1/index.html

Django REST framework

Introduction

Django REST framework is a lightweight REST framework for Django, that aims to make it easy to build well-connected, self-describing RESTful Web APIs.

Browse example APIs created with Django REST framework: The Sandbox

Features:

Resources

Project hosting: GitHub.

Any and all questions, thoughts, bug reports and contributions are hugely appreciated.

Requirements

  • Python (2.5, 2.6, 2.7 supported)
  • Django (1.2, 1.3, 1.4-alpha supported)

Installation

You can install Django REST framework using pip or easy_install:

pip install djangorestframework

Or get the latest development version using git:

git clone git@github.com:tomchristie/django-rest-framework.git

Or you can download the current release.

Setup

To add Django REST framework to a Django project:

  • Ensure that the djangorestframework directory is on your PYTHONPATH.
  • Add djangorestframework to your INSTALLED_APPS.

For more information on settings take a look at the Setup section.

Getting Started

Using Django REST framework can be as simple as adding a few lines to your urlconf.

The following example exposes your MyModel model through an api. It will provide two views:

  • A view which lists your model instances and simultaniously allows creation of instances from that view.
  • Another view which lets you view, update or delete your model instances individually.

urls.py:

from django.conf.urls.defaults import patterns, url
from djangorestframework.resources import ModelResource
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
from myapp.models import MyModel

class MyResource(ModelResource):
    model = MyModel

urlpatterns = patterns('',
    url(r'^$', ListOrCreateModelView.as_view(resource=MyResource)),
    url(r'^(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MyResource)),
)

Examples

There are a few real world web API examples included with Django REST framework.

  1. Object Store API - Using views.View classes for APIs that do not map to models.
  2. Code Highlighting API - Using views.View classes with forms for input validation.
  3. Blog Posts API - Using views.ModelView classes for APIs that map directly to models.

All the examples are freely available for testing in the sandbox:

http://rest.ep.io

(The Sandbox Root API resource is also documented.)

Indices and tables


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值