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

相关推荐
爬山算法5 分钟前
Oracle(129) 如何使用闪回归档(Flashback Archive)?
数据库·oracle
Kk.61634 分钟前
SQL编程题复习(24/9/20)
数据库·sql·pta考试·专本贯通
Hello.Reader1 小时前
ClickHouse 与 Quickwit 集成实现高效查询
python·clickhouse·django·全文检索
Yz98761 小时前
Hadoop-MapReduce的 原理 | 块和片 | Shuffle 过程 | Combiner
大数据·数据库·数据仓库·hadoop·mapreduce·big data
加油=^_^=1 小时前
MySQL基础篇的补充
数据库·python·mysql
porkczr2 小时前
oracle rac多个实例就相当于多个数据库系统程序
数据库·oracle
大白菜和MySQL2 小时前
mysql mha高可用集群搭建
数据库·mysql
QQ爱剪辑2 小时前
MySQL基础(13)- MySQL数据类型
数据库·mysql
后端小张3 小时前
Redis 执行 Lua,能保证原子性吗?
数据库·redis·缓存
离开地球表面_994 小时前
索引失效?查询结果不正确?原来都是隐式转换惹的祸
数据库·后端·mysql