码迷,mamicode.com
首页 > 其他好文 > 详细

autotes开发文档

时间:2016-10-30 19:43:01      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:eth   led   ica   ase   miss   default   style   else   strong   

开发环境: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 = 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=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开发文档

标签:eth   led   ica   ase   miss   default   style   else   strong   

原文地址:http://www.cnblogs.com/liqxd/p/6013440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!