python 之路,Django rest framework 初探
Django rest framework介绍Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework:
安装REST framework requires the following:
The following packages are optional:
Install using ...or clone the project from github. Add 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 Note that the URL path can be whatever you want,but you must include <h3 id="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 information on the users of our project. Any global settings for a REST framework API are kept in a single configuration dictionary named Don't forget to make sure you've also added We're ready to create our API now. Here's our project's root Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer): ViewSets define the view behavior.class UserViewSet(viewsets.ModelViewSet): Routers provide an easy way of automatically determining the URL conf.router = routers.DefaultRouter() Wire up our API using automatic URL routing.Additionally,we include login URLs for the browsable API.urlpatterns = [ You can now open the API in your browser at,and view your new 'users' API. If you use the login control in the top right corner you'll also be able to add,create and delete users from the system. Django视图中使用restfrom django.http import HttpResponse,JsonResponsefrom django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from rest_framework.decorators import api_view from rest_framework import status class EventLogSerializer(serializers.ModelSerializer): @api_view(['GET','POST'])
@api_view(['GET','POST','PUT'])@csrf_exempt
更多请看http://www.django-rest-framework.org/ (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |