环境准备

pip install django
pip install djangorestframework
pip install pygments  # 用于代码高亮

开始项目

创建项目

和创建普通 django 项目一样创建一个新项目,名称为”tutorial”。

cd ~
django-admin.py startproject tutorial
cd tutorial

然后,再创建一个名为”snippets”的 app

python manage.py startapp snippets

配置项目

snippetsrest_framework 添加到 setting.py 文件的 INSTALL_APP 里面去,即打开 tutorial/setting.py 文件,添加两项:

INSTALLED_APPS = (
    ...
    'rest_framework',
    'snippets',
)

然后再编辑

tutorial/urls.py

文件,我的配置是这样的:

urlpatterns = [
    ...
    url(r'^snippets/$', resttest.views.snippet_list),
    url(r'^snippets/(?P<pk>[0-9]+)/$', resttest.views.snippet_detail),
]

编写项目

创建模型

这里只创建了一个名为 snippets 的模型,打开 snippets/models.py ,内容如下:

from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())

class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES,
                                default='python',
                                max_length=100)
    style = models.CharField(choices=STYLE_CHOICES,
                             default='friendly',
                             max_length=100)

    class Meta:
        ordering = ('created',)

然后同步一下,

python manage.py makemigrations snippets
python manage.py migrate
创建系列化类

因为我们要实现的是 rest 风格的 api ,所以需要对对象进行序列化,最好是序列化成 json,那么这个过程就需要编写一个可以帮助我们序列化类的类,在 snippets 目录下创建一个名为 serializers.py 的 python 文件,内容如下:

from django.forms import widgets
from rest_framework import serializers
from resttest.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES

class SnippetSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

    def create(self, validated_data):
        # Create and return a new Snippet instance, given the validated data.
        return Snippet.objects.create(**validated_data)

    def update(self, instance, validated_data):
        # Update and return an existing Snippet instance, given the validated data.
        instance.title = validated_data.get('title', instance.title)
        instance.code = validated_data.get('code', instance.code)
        instance.linenos = validated_data.get('linenos', instance.linenos)
        instance.language = validated_data.get('language', instance.language)
        instance.style = validated_data.get('style', instance.style)
        instance.save()
        return instance
编写视图

api 的核心处理单元就是视图了,现在就来编写视图的代码,打开 snippets/views.py ,内容如下:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from resttest.models import Snippet
from resttest.serializers import SnippetSerializer


class JSONResponse(HttpResponse):
    # An HttpResponse that renders its content into JSON.

    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def snippet_list(request):
    # List all code snippets, or create a new snippet.

    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)

@csrf_exempt
def snippet_detail(request, pk):
    # Retrieve, update or delete a code snippet.
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)
    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JSONResponse(serializer.data)
    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)
    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204)

csrf_exempt 因为我们支持 POST 类型,并且包含敏感字符,但是因为我们没有 csrf_token ,所以不能使用 django 的 csrf 防御机制,因此,这里需要使用这个装饰器进行处理。

测试项目

添加数据

切换到 tutorial 目录,然后使用命令: python manage.py shell 进入 django 交互式窗口,然后按照下面的内容输入:

from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

snippet = Snippet(code='foo = barn')
snippet.save()

snippet = Snippet(code='print hello, worldn')
snippet.save()

完成!

启动服务并验证

切换到 tutorial 目录,然后使用命令:python manage.py runserver 打开浏览器,输入网址 : http://localhost:8000