Python Monitor Water Falls(2)Python RESTful Service

Python Monitor Water Falls(2)Python RESTful Service

Prepare the package
>pip install django
>pip install djangorestframework
browsable API
>pip install markdown
filter support
>pip install django-filter

Start up the project
>django-admin.py startproject restful_api
>cd restful_api/
Create a quick start
>django-admin.py startapp quickstart

Sync the database for the first time
>python manage.py migrate

Create a user admin and password password123.
>python manage.py createsuperuser --email [email protected] --username admin
It will ask me for password.

First Example to Build RESTful Service on that.
Add restful_api/quickstart/serializers.py
from django.contrib.auth.models import User, Group
from rest_framework import serializers

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')

Viewsets to Have the Response in restful_api/quickstart/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from restful_api.quickstart.serializers import UserSerializer, GroupSerializer

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint for users
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer

class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint for groups
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

Add the URL mapping in restful_api/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from restful_api.quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Add the settings there in restful_api/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]

Start the RESTful Service
>python manage.py runserver

Test that from CURL
>curl -H 'Accept: application/json;indent=4' http://localhost:8000/users/
[
    {
        "url": "http://localhost:8000/users/1/",
        "username": "admin",
        "email": "[email protected]",
        "groups": []
    }
]

Test that from Browsable API
http://localhost:8000/

That is a very nice beginning.
Here are more to read about some details
http://www.django-rest-framework.org/tutorial/1-serialization/
http://www.django-rest-framework.org/tutorial/2-requests-and-responses/
http://www.django-rest-framework.org/tutorial/3-class-based-views/

And further read in settings
http://www.django-rest-framework.org/api-guide/settings/

References:
http://www.django-rest-framework.org/
http://jadyer.cn/2013/03/19/xml-stax/
https://blog.windrunner.me/python/web/django-rest-framework.html
http://www.cnblogs.com/ccorz/p/djangorestframework-shi-yong-li-zi.html
http://blog.csdn.net/luojie140/article/details/76832749




猜你喜欢

转载自sillycat.iteye.com/blog/2408979