1. Preparations
- Install django framework
- Install the django-rsetful framework
pip install djangorestframework
- 1
2. A small demo
2.1 Create a django project
django admin startproject restful
django admin startapp demo
- 1
- 2
- 3
2.2 Configure restful environment
修改restful/settings.py
在INSTALLED_APPS 添加 'rest_framework'
- 1
- 2
- 3
2.3 Write the model layer/demo/model.py
from django.db import models
# Create your models here.
class User(models.Model):
username = models.CharField(max_length=20,blank=True)
password = models.CharField(max_length=20,blank=True)
def __str__(self):
return self.username
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.4 Configuring the database
python manage.py makemigratons
python manage.py migrate
python manage.py createsuperuser
- 1
- 2
- 3
- 4
2.5 Configure background management /demo/admin.py
from django.contrib import admin
# Register your models here.
from demo.models import User
admin.site.register(User)
- 1
- 2
- 3
- 4
- 5
- 6
At this time, log in to the background to add some data
(if the picture is unclear, press Ctrl + mouse wheel to zoom in)
2.6 Write the serializer module /demo/serializer.py
# author: HuYong
# coding=utf-8
from rest_framework import serializers
from demo.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("id","username","password")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Serialization connects json and model layer
2.7 Write url
2.7.1 root url restful/url.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/demo/',include('demo.urls')),
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.7.2APP URL demo/urls.py
# author: HuYong
# coding=utf-8
from django.conf.urls import url
from demo import views
urlpatterns = [
url(r'^user/$',views.User_list),
url(r'^user/(?P<pk>[0-9]+)/$',views.User_detial),
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.8 Write the views layer demo/views.py
import json
from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from demo.models import User
from demo.serializer import UserSerializer
@api_view(['GET','POST'])
def User_list(request):
if request.method=="GET":
users = User.objects.all()
serializer = UserSerializer(users,many=True)
return Response(serializer.data)
elif request.method == 'POST':
print request.body
serializer = UserSerializer(data=json.loads(request.body))
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'PUT', 'DELETE']) #
def User_detial(request,pk):
try:
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
serializer = UserSerializer(user)
return Response(serializer.data)
elif request.method == "PUT":
serializer = UserSerializer(user,data=json.loads(request.body))
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
else:
return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
elif request.method == "DELETE":
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
2.9 Testing
2.9.1 Get user list
- Browser access http://127.0.0.1:8000/api/demo/user/
This browser access interface - Tested by postman, it returns json data
2.9.2 New data
- Test with postman
- Simple setup
- Returned data:
- Get the user list bar at this point
2.9.3 Operations on a single user
- URL http://127.0.0.1:8000/api/demo/user/4 (user with id 4)
- Get user information (GET)
- Modify User Information (PUT)
- Delete user information (DELETE)