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

Out9: 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

Out10: 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/(?P0-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\\[0-9{4})/(?P<month>0-9{2})/ ′,views.yeararchive),articles/1998/repath(r′articles/(?P\\[0−94)/(?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

相关推荐
企服AI产品测评局1 小时前
Agent适配信创环境实测:企业级自动化如何实现国产操作系统与数据库全兼容?
运维·数据库·人工智能·ai·chatgpt·自动化
cfm_29141 小时前
Redis数据安全性解析
数据库·redis·缓存
DIY源码阁1 小时前
JavaSwing学生成绩管理系统 - MySQL版
java·数据库·mysql·eclipse
NiceCloud喜云3 小时前
Claude Code Routines 实战:三种触发器跑通云端自动化编码
android·运维·数据库·人工智能·自动化·json·飞书
辞忧九千七3 小时前
Redis 单机一主二从主从复制完整搭建指南
数据库·redis·缓存
lzhdim3 小时前
SQL 入门 16:SQL 事务隔离级别与死锁解析(易懂)
数据库·sql
AI 小老六4 小时前
Claude Code 如何压缩上下文:Microcompact、Prompt Cache 与 cache_edits 工程拆解
数据库·人工智能·ai·语言模型·架构·系统架构
Chasing__Dreams4 小时前
Redis--基础知识点--32--redis底层存储结构
数据库·redis·缓存
不总是4 小时前
[2026最新] Windows 免安装版 MySQL 8 详细安装配置教程(ZIP 压缩包版)
数据库·windows·mysql
tedcloud1235 小时前
DBX部署教程:打造支持AI SQL助手的数据库管理环境
数据库·人工智能·sql