Django REST framework¶

Django REST framework

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

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

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)), )

原文地址:https://www.cnblogs.com/lexus/p/2487012.html