網站首頁 Vue 正文
前言:接口測試平臺,后端使用django,前端使用vue+element。項目接口平臺測試完成,需要把后臺產生的測試報告返回給前端展示。
一、后端接口
????1、配置下django的template的參數,templates文件夾是放在project的目錄下面的,是項目中或者說項目中所有的應用公用的一些模板
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'userfiles')], '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', ], }, }, ]
2、視圖函數,讀取template目錄下report文件夾的報告,并返回給前端
def getReport(self, request): # 獲取POST BODY信息 reportId = request.data["id"] try: print(os.getcwd()) # 打印程序運行目錄 template = loader.get_template(f"./report/{reportId}.html") # template = loader.get_template(f"1.html") return Response(template.render()) # return HttpResponse(template.render()) except Exception as e: content = {'message': '獲取報告失敗'} return Response(content)
二、前端
1、如果后端返回的HTML文件不包含js文件,可以使用vue的v-html,vue的v-html只能渲染html元素和css文件,,不能執行js文件
2、后端返回的數據,HTML文件包含js文件。使用下面這種方法,接口獲取到的html數據在暫存的本地localStorage,再讀取數據,然后在新窗口打開報告。
接口返回的數據如下:
?template:
<el-button type="warning" @click="viewReport" :disabled="reportDisabled
methods:
// 查看測試報告 viewReport () { var message = {id:1} // axios 通過接口獲取后臺的報html告文件數據 getReport(message).then(res => { this.$message({ showClose: true, message: res.data.message, type: 'success' }) // res.data 為接口返回的html完整文件代碼 // 必須要存進localstorage,否則會報錯,顯示不完全 window.localStorage.removeItem('callbackHTML') window.localStorage.setItem('callbackHTML', res.data) // 讀取本地保存的html數據,使用新窗口打開 var newWin = window.open('', '_blank') newWin.document.write(localStorage.getItem('callbackHTML')) // 關閉輸出流 newWin.document.close() }).catch(err => { this.$message({ showClose: true, message: err.response.msg, type: 'error' }) }) }
至此結束,點擊下按鈕即可在新窗口展示測試報告了
原文鏈接:https://blog.csdn.net/wuyuezhaolu/article/details/120452840
相關推薦
- 2022-07-31 教你用python實現一個加密的文字處理器_python
- 2022-10-11 Filter過濾器和Listener監聽器
- 2022-09-14 Python詳細講解淺拷貝與深拷貝的使用_python
- 2024-03-14 AOP切面編程,以及自定義注解實現切面
- 2022-08-23 多線程python的實現及多線程有序性_python
- 2022-08-12 Android自定義彈出框的方法_Android
- 2022-07-14 AVX2指令集優化整形數組求和算法_C 語言
- 2022-04-05 Python利用prettytable庫輸出好看的表格_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支