首页 > 代码库 > 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开发文档
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。