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 luohuazju@gmail.com --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": "luohuazju@gmail.com",
"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.youkuaiyun.com/luojie140/article/details/76832749
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值