我恐怕是找到了接口测试范文吧?大佬认真起来真可怕!
Posted 51Testing软件测试网
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我恐怕是找到了接口测试范文吧?大佬认真起来真可怕!相关的知识,希望对你有一定的参考价值。
(1)Django 安装
实践:输入 Django-admin.py 运行成功
(2)Django rest framework 安装
(3)项目创建
d 盘创建新项目
命令:Django-admin startproject Django_restful
Django_restful 项目中创建 api 应用
命令:python manage.py startapp api
进入 D:\Django_restful\Django_restful 目录下,打开 setting.py 文件创建 api 和 rest_framework
配置 rest_framewor 权限
在 setting.py 文件末尾添加权限代码:
REST_FRAMEWORK = {
DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}
(4)数据库迁移
cmd下输入命令:python manage.py migrate
提示如下图迁移成功
(5)创建超级管理员
cmd下输入命令:python manage.py createsuperuser
依次输入账号、邮箱、密码,如下图
(6)启动server
启动Django ,登录创建的用户python manage.py runserver
(7)登录超级管理员账户
输入账号、密码后登录
(8)数据序列化
在API文件夹下新建serializers.py文件,定义API返回形式,返回哪些字段,返回怎样的格式等
代码:
# from Django.contrib.auth.models import User,Group
from rest_framework import serializers
from api.models import User,Group
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model=User
fields=('url','username','email','groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model=Group
fields=('url','name')
(9)创建视图
Django rest framework 使用ViewSets定义视图的展现形式,如何向用户展示数据,展示什么数据等
在 Api下vies.py文件编写如下代码
from Django.shortcuts import render
# from Django.contrib.auth.models import User,Group
from rest_framework import viewsets
from api.serializers import UserSerializer,GroupSerializer
from api.models import User,Group
# Create your views here.
class UserViewSet(viewsets.ModelViewSet):
"""
retrieve:
Return a user instance.
list:
Return all users,odered by most recent joined.
create:
Create a new user.
delete:
Remove a existing user
partial_update:
Update one or more fields on a existing user.
update:
Update a user.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
retrieve:
Return a group instance.
list:
Return all groups, ordered by most recently joined.
create:
Create a new group.
delete:
Remove an existing group.
partial_update:
Update one or more fields on an existing group.
update:
Update a group.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
(10)URL路由配置
打开Django_restful/urls.py文件,填写下面的代码配置路由
"""Django_restful URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.Djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from Django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from Django.contrib import admin
from Django.urls import path
from Django.conf.urls import include
from rest_framework import routers
from api import views
#from rest_framework.schemas import get_schema_view
#from rest_framework_swagger.renderers import SwaggerUIRenderer,OpenAPIRenderer
#schema_view=get_schema_view(title='API',renderer_classes=[OpenAPIRenderer,SwaggerUIRenderer])
router=routers.DefaultRouter()
router.register(r'users',views.UserViewSet)
router.register(r'groups',views.GroupViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('',include(router.urls)),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
path('docs/',schema_view,name='docs')
]
cmd下重新启动服务:python manage.py runserver
# from Django.contrib.auth.models import User,Group
from rest_framework import serializers
from api.models import User,Group
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model=User
fields=('url','username','email','groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model=Group
fields=('url','name')
立即扫描二维码阅读全文
点击阅读☞
点击阅读☞
点击阅读☞
点击阅读☞
点击阅读☞
以上是关于我恐怕是找到了接口测试范文吧?大佬认真起来真可怕!的主要内容,如果未能解决你的问题,请参考以下文章