Django REST架構實現

來源:互聯網
上載者:User

安裝Django REST.

安裝條件:

  • Python (2.5, 2.6, 2.7 supported)
  • Django (1.2, 1.3, 1.4 supported)
  • django.contrib.staticfiles (or django-staticfiles for
    Django 1.2)
  • URLObject >= 2.0.0
  • Markdown >= 2.1.0 (Optional)
  • PyYAML >= 3.10 (Optional)

使用pip或者easy_install

pip install djangorestframework
easy_install install djangorestframework

或者擷取最新開發版

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

修改settings.py:

INSTALLED_APPS 中添加

    'djangorestframework',

修改urls.py:

from django.conf.urls.defaults import patterns, include, urlfrom djangorestframework.compat import Viewfrom djangorestframework.mixins import ResponseMixinfrom djangorestframework.renderers import DEFAULT_RENDERERSfrom djangorestframework.response import Responsefrom django.core.urlresolvers import reversefrom django.contrib import adminadmin.autodiscover()# Uncomment the next two lines to enable the admin:# from django.contrib import admin# admin.autodiscover()class ExampleView(ResponseMixin,View):        renderers=DEFAULT_RENDERERS        def get(self,request):                response=Response(200,{'description':'Some example content',                        'url':reverse('mixin-view')})                return self.render(response)urlpatterns = patterns('',    # Examples:    # url(r'^$', 'djrest.views.home', name='home'),    # url(r'^djrest/', include('djrest.foo.urls')),    # Uncomment the admin/doc line below to enable admin documentation:    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),    # Uncomment the next line to enable the admin:    # url(r'^admin/', include(admin.site.urls)),    url(r'^admin/',include(admin.site.urls)),    url(r'^$',ExampleView.as_view(),name='mixin-view'),    url(r'^demo/$','depot.views.detail'),)

測試:

Django REST Framework官網:

http://django-rest-framework.org

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.