首页 > 代码库 > autotes开发文档

autotes开发文档

开发环境:django1.10 + djangoRestframework3.5 + python3.5 + angularJs

环境设置:

django-restframework

INSTALLED_APPS = (
    ...
    rest_framework,
     ) REST_FRAMEWORK
= { DEFAULT_PERMISSION_CLASSES: (rest_framework.permissions.IsAdminUser,), PAGE_SIZE: 10 }

django-database:

DATABASES = {
    default: {
        ENGINE: django.db.backends.sqlite3,
        NAME: os.path.join(BASE_DIR, db.sqlite3),
    }
}

 

API

  登陆:api/loginUser

  返回结果:

  实现:

  serializers:

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

  views:

@api_view([GET])
# @permission_classes((permissions.AllowAny,))
def loginUser(request):
    if request.method == GET:
        #获取
        data =http://www.mamicode.com/ request.query_params
        username = data[username]
        password = data[password]
        #用户验证并返回user对象
        user = authenticate(username=username, password=password)
        #实例化序列化类
        serializer =  UserSerializer(user)
        #返回json数据
        return Response(serializer.data)

 

 

  注册:api/registUser

  返回结果:

  实现:

  serializers:

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

  views:

@api_view([GET,POST])
def registUser(request):
    if request.method == POST:
        print(wtf)
        serializer = UserSerializer(data=http://www.mamicode.com/request.data)
        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)

  

前台:

 

autotes开发文档