django使用总结

django框架
pycharm下方的terminal常用命令
创建app:python manage.py startapp app
启动django服务器:python manage.py runserver 127.0.0.1:8000
models中建表
创建migrations 文件:python manage.py makemigrations
数据同步到数据库:python manage.py migrate
数据反向生成models.py
python manage.py inspectdb > apiTest/models.py
models.py
class Testcaseapi(models.Model):
name = models.CharField(max_length=200)
version = models.CharField(max_length=100)
the_module = models.CharField(max_length=20)
execuflag = models.IntegerField(db_column='execuFlag') # Field name made lowercase.
executionsequence = models.CharField(db_column='executionSequence', max_length=50) # Field name made lowercase.
needtestid = models.CharField(db_column='needTestId', max_length=50) # Field name made lowercase.
delflag = models.IntegerField(db_column='delFlag') # Field name made lowercase.
createtime = models.DateTimeField(db_column='createTime') # Field name made lowercase.
createuser = models.CharField(db_column='createUser', max_length=50) # Field name made lowercase.
updatetime = models.DateTimeField(db_column='updateTime', blank=True, null=True) # Field name made lowercase.
updateuser = models.CharField(db_column='updateUser', max_length=50) # Field name made lowercase.

class Meta:
managed = False
db_table = 'testCaseApi'
路由
urls.py
urlpatterns = [
#通配html地址
url(r'^(.*).html',view=views.myFun.apitest01),

#添加
url(r'addApi/',view=views.myFun.addApi,name='addApi'),

#编辑
url(r'editApi/',view=views.myFun.editApi,name='editApi'),

#编辑按钮获取单个接口数据,填充到模态框展示
url(r'getApi/',view=views.myFun.getApi,name='getApi'),

#删除
url(r'delApi/',view=views.myFun.delApiData,name='delApiData'),

#加载接口列表,包含全部查询与搜索功能 apiList.html
url(r'apiList/',view=views.myFun.apiList,name='apiList'),
]
views.py
#添加测试接口标,接口步骤表,从前端获取数据
def addApi(request):
theModule=request.GET.get('theModule')
interfaceName=request.GET.get('interfaceName')
reqType=request.GET.get('reqType')
precondition=request.GET.get('precondition')
url=request.GET.get('url')
parameters=request.GET.get('parameters')
assertData=request.GET.get('assertData')
print('是否拿到所属模块数据:',theModule)
teseCaseDatabase().addTestApi(theModule=theModule,interfaceName=interfaceName,url=url,parameters=parameters,precondition=precondition,reqType=reqType,assertData=assertData)
return HttpResponse(json.dumps({"success":"success"}),content_type='application/json')
路由映射原理
第一步:前端页面从urls.py的urlpatterns中读取接口路径
第二部:找到对应接口绑定的方法,该方法写在views.py中,绑定方式(view=views.myFun.apitest01)
项目配置settings.py
app注册
#注册app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apiTest',
]
数据库配置
#数据库地址配置 本地环境
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'autoTest',
'USER':'root',
'PASSWORD':'',
'HOST':'172.21.0.209',
}
}
html文件路径
#HTML文件路径templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Static files
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#需要修改
STATIC_URL = '/static/'
#定义全局变量
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
#os.path.join(os.path.dirname(__file__), '../static/').replace('\\', '/'),
)

猜你喜欢

转载自www.cnblogs.com/qinhaili/p/9184362.html