Django测试工具 Client

Django测试工具 Client

Python3 manage.py shell

1.测试模型中的数据操作

from OverlayList.models import RequestInfo

RequestInfo.objects.all()

2.测试URL视图,返回值

$Python3 manage.py shell

from django.test.utils import setup_test_environment

setup_test_environment()

from django.test import Client

create an instance of the client for our use

client = Client()

In [8]: response = client.get('/stage?project=D28')

In [9]: response.content

Out[9]: b'{"list": [{"model": "Common.stage", "pk": 1, "fields": {"name": "P1", "description": "", "startTime": null, "stopTime": null, "project": "D28"}}, {"model": "Common.stage", "pk": 3, "fields": {"name": "P2", "description": "", "startTime": null, "stopTime": null, "project": "D28"}}], "msg": "success", "error_num": 0}'

In [10]: response.status_code

Out[10]: 200

Django URL

一、URL调度器

给应用设计URL,需要创建一个python模块,即URLconf, 包含URL模式到python函数(视图)的简单映射。

1.Path()/re_path()

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
  3. Add an import: from other_app.views import Home
  4. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
    Including another URLconf
  5. Import the include() function: from django.urls import include, path
  6. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  • 要从 URL 中取值,使用尖括号。

  • 这里不需要添加反斜杠,因为每个 URL 都有。比如,应该是 articles 而不是 /articles 。

  • path('articles/int:year/int:month/slug:slug', views.article_detail),

  • /articles/2003/03/building-a-django-site/ 会匹配 URL 列表中的最后一项。Django 会调用函数 views.article_detail(request, year=2003, month=3, slug="building-a-django-site") 。

2.路径转换器

  • str - 匹配除了 '/' 之外的非空字符串。如果表达式内不包含转换器,则会默认匹配字符串。
  • int - 匹配 0 或任何正整数。返回一个 int 。
  • slug - 匹配任意由 ASCII 字母或数字以及连字符和下划线组成的短标签。比如,building-your-1st-django-site 。
  • uuid - 匹配一个格式化的 UUID 。为了防止多个 URL 映射到同一个页面,必须包含破折号并且字符都为小写。比如,075194d3-6885-417e-a8a8-6c931e272f00。返回一个 UUID 实例。
  • path - 匹配非空字段,包括路径分隔符 '/' 。它允许你匹配完整的 URL 路径而不是像 str 那样匹配 URL 的一部分。

3.使用正则表达式,re_path()

语法是(?Ppattern)

path('articles/2003/', views.special_case_2003),

re_path(r'^articles/(?P[0-9]{4})/ ′ , v i e w s . y e a r a r c h i v e ) , a r t i c l e s / 1998 / r e p a t h ( r ′ a r t i c l e s / ( ? P < y e a r > [ 0 − 9 ] 4 ) / ( ? P < m o n t h > [ 0 − 92 ) / ', views.year_archive), articles/1998/ re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9{2})/ ′,views.yeararchive),articles/1998/repath(r′articles/(?P<year>[0−9]4)/(?P<month>[0−92)/', views.month_archive), articles/1998/

4.指定视图参数的默认值

path('blog/', views.page), 使用默认的num参数,1

path('blog/pageint:num/', views.page),

def page(request, num=1):

pass

5.包含其他的URLconf, include()

path('community/', include('aggregator.urls')),

二、nginx代理请求:

proxy代理,对应到生产环境,需要在Nginx配置代理,转发请求

build打包后的dist文件直接放到服务器上,进行配置

vue搭建一个本地服务器运行打包后dist文件

1.npm install -g http-server

2.开启服务: http-server ./dist

3.会出现访问地址,浏览器预览即可

三、打包成app

用能在本地打开的dist文件夾,成功打包APK

想要直接在本地打开dist文件,直接看到页面效果,可以根据如下操作进行:

參考

解决vue项目打包后打开index.html一片空白

proxy代理請求部分,可以配置生产环境和开发环境的地址

/config/index.js

proxyTable: {

'/film': {

target: 'https://m.maizuo.com',

changeOrigin: true,

pathRewrite: {

'^/film':

'https://m.maizuo.com'

}

},

},

/config/dev.env.js

module.exports = merge(prodEnv, {

NODE_ENV: '"development"',

API_HOST:'"/film"'

})

/config/prod.env.js

module.exports = {

NODE_ENV: '"production"',

API_HOST:'"https://m.maizuo.com"'

}

axios请求部分

添加baseURL, 根据时环境,自由切换

baseURL: process.env.API_HOST // 兼容开发环境和生产环境的请求地址

axios.get('/gateway')

npm run dev : /film/gateway

==> 转发 https://m.maizuo.com/gateway

npm run build : https://m.maizuo.com/gateway

相关推荐
七夜zippoe17 小时前
# DolphinDB分区策略:RANGE分区详解
数据库·策略·分区·range·dolphindb
地球资源数据云18 小时前
中国陆地生态系统主要植物功能特征空间分布数据
大数据·数据库·人工智能·机器学习
吠品18 小时前
高性能JS数组操作:何时选用push、unshift、splice或扩展运算符?
linux·服务器·数据库
洛水水18 小时前
【Redis入门】一篇详解Redis五大数据结构
数据结构·数据库·redis
woodykissme18 小时前
日产汽车花键测绘,为什么总踩坑?
数据库·汽车·齿轮·渐开线花键
雨辰AI18 小时前
从 MySQL 迁移至人大金仓 V9 完整改造指南|分页 / 函数 / 语法兼容全部解决
java·开发语言·数据库·后端·mysql·政务
Chengbei1118 小时前
AI大模型网关存在SQL注入、影响版本LiteLLM 1.81.16~1.83.7(CVE-2026-42208)
数据库·人工智能·sql·安全·web安全·网络安全·系统安全
zx28596340018 小时前
Laravel 9.x:全面升级与核心特性
数据库
努力努力再努力wz19 小时前
【MySQL进阶系列】一文打通事务机制:从锁、Undo Log 到 MVCC 与隔离级别
c语言·数据结构·数据库·c++·mysql·算法·github
PaperData19 小时前
2000-2025年《中国县域统计年鉴》pdf+excel版(附赠面板数据)
数据库·人工智能·数据分析·pdf·经管