当前位置: 首页 > news >正文

8、添加第三方包

目录

  • 1、安装Django Debug Toolbar

Django的一个优势就是有丰富的第三方包生态系统。这些由社区开发的包,可以用来快速扩展应用程序的功能集

1、安装Django Debug Toolbar

Django Debug Toolbar位于名列前三的第三方包之一
这是一个用于调试Debug Web应用程序的有用工具。该工具帮助我们了解应用的运行方式并发现问题。它通过提供面板来提供有关当前请求和响应的调试信息
在已激活的虚拟环境中运行以下命令来安装包

py -m pip install django-debug-toolbar

在这里插入图片描述
与django集成的第三方包需要一些安装后的设置,以将它们与我们的项目整合在一起。我们需要将包的Django应用程序添加到你的INSTALLED_APPS设置中。有些包需要其他更改,比如添加到我们的URL配置中。
链接: 安装指南
在polls/settings中添加
在这里插入图片描述
在项目URLconf中添加:
在这里插入图片描述
添加中间件polls/settings
在这里插入图片描述
polls/settings

INTERNAL_IPS=["127.0.0.1",
]

如果要在项目中运行测试,则不应激活工具栏。你 可以通过添加另一个设置来执行此操作

polls/settings

TESTING="test"in sys.argv
if not TESTING:INSTALLED_APPS=[*INSTALLED_APPS,#* 符号用于将一个列表或元组中的所有元素解包,并将其插入到另一个列表或元组中"debug_toolbar",]MIDDLEWARE=["debug_toolbar.middleware.DebugToolbarMiddleware",*MIDDLEWARE,]

URLconf:

from django.conf import  settings
if not settings.TESTING:urlpatterns=[*urlpatterns,]+debug_toolbar_urls()

使用 * 将列表或元组中的元素作为单独的参数传递给函数:
def add(a, b, c): return a + b + c numbers = [1, 2, 3] result = add(*numbers) # 相当于 add(1, 2, 3) print(result) # 输出 6

使用 * 将一个列表或元组中的所有元素插入到另一个列表或元组中:
list1 = [1, 2, 3] list2 = [4, 5, 6] combined = [*list1, *list2] print(combined) # 输出 [1, 2, 3, 4, 5, 6]

总之最终的settings

"""
Django settings for vote project.Generated by 'django-admin startproject' using Django 5.0.6.For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
import sys
from pathlib import Path# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-g%c$9$3_-z8znkcj+qdf=oki+0m!y$7d8anr#i)%bcfq(#iq#l"# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = []
INTERNAL_IPS=["127.0.0.1",
]# Application definitionINSTALLED_APPS = [# "debug_toolbar","polls.apps.PollsConfig","django.contrib.admin",#管理员站点"django.contrib.auth",#认证授权系统"django.contrib.contenttypes",#内容类型框架"django.contrib.sessions",#会话框架"django.contrib.messages",#消息框架"django.contrib.staticfiles",#管理静态文件的框架
]MIDDLEWARE = [# "debug_toobar.middleware.DebugToolbarMiddleware","django.middleware.security.SecurityMiddleware","django.contrib.sessions.middleware.SessionMiddleware","django.middleware.common.CommonMiddleware","django.middleware.csrf.CsrfViewMiddleware","django.contrib.auth.middleware.AuthenticationMiddleware","django.contrib.messages.middleware.MessageMiddleware","django.middleware.clickjacking.XFrameOptionsMiddleware",
]
DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.versions.VersionsPanel","debug_toolbar.panels.timer.TimerPanel","debug_toolbar.panels.settings.SettingsPanel","debug_toolbar.panels.headers.HeadersPanel","debug_toolbar.panels.request.RequestPanel","debug_toolbar.panels.sql.SQLPanel","debug_toolbar.panels.staticfiles.StaticFilesPanel","debug_toolbar.panels.templates.TemplatesPanel","debug_toolbar.panels.cache.CachePanel","debug_toolbar.panels.signals.SignalsPanel","debug_toolbar.panels.logging.LoggingPanel","debug_toolbar.panels.redirects.RedirectsPanel",]
ROOT_URLCONF = "vote.urls"TEMPLATES = [{"BACKEND": "django.template.backends.django.DjangoTemplates","DIRS": [BASE_DIR/"templates"],#在Django载入模板时使用,是一个待搜索路径"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",],},},
]WSGI_APPLICATION = "vote.wsgi.application"
## Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databasesDATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3","NAME": BASE_DIR / "db.sqlite3",}
}# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validatorsAUTH_PASSWORD_VALIDATORS = [{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",},{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/LANGUAGE_CODE = "zh-hans"TIME_ZONE = "Asia/Shanghai"USE_I18N = True
USE_L10N = True
USE_TZ = TrueLANGUAGES = [('en', 'English'),('zh-hans', '简体中文'),
]# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/STATIC_URL = "/static/"
TESTING="test" in sys.argv
if not TESTING:INSTALLED_APPS +=[# *INSTALLED_APPS,#* 符号用于将一个列表或元组中的所有元素解包,并将其插入到另一个列表或元组中"debug_toolbar",]MIDDLEWARE=["debug_toolbar.middleware.DebugToolbarMiddleware",# *MIDDLEWARE,]+MIDDLEWARE
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

vote.urls

from django.contrib import admin
from django.urls import include,path
import debug_toolbar
from django.conf import  settings
urlpatterns = [path("polls/",include("polls.urls")),path("admin/", admin.site.urls),
]if not settings.TESTING:urlpatterns +=[path('__debug__/',include(debug_toolbar.urls)),]

页面右方会显示面板
在这里插入图片描述
在这里插入图片描述

其他内容可参考
链接: 第8节 添加第三方包

相关文章:

  • Android --- Kotlin学习之路:协程的使用,什么是协程,为什么要用协程?(学习笔记)
  • Docker 和 k8s 之间是什么关系?
  • 通义千问AI模型对接飞书机器人-模型配置(2-1)
  • HarmonyOS ArkUi @CustomDialog 和promptAction.openCustomDialog踩坑以及如何选择
  • Python--PyMySQL 库基础操作笔记
  • LeetCode热题100(JavaScript)
  • HTTP状态码(HTTP Status Code)讲解
  • k8s上部署openvpn
  • IP地址:由电脑还是网线决定?
  • 【产品评测】海康威视(HIKVISION)NAS网络存储——简单评测
  • PostgreSQL安装/卸载(CentOS、Windows)
  • docker 部署wechatbot-webhook 并获取接口实现微信群图片自动保存到chevereto图库等
  • 计算机网络入门 -- 常用网络协议
  • el-menu弹出菜单样式不生效
  • 十一、数组(1)
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • Asm.js的简单介绍
  • canvas 绘制双线技巧
  • css系列之关于字体的事
  • HTML中设置input等文本框为不可操作
  • JavaScript中的对象个人分享
  • Joomla 2.x, 3.x useful code cheatsheet
  • Spring Boot MyBatis配置多种数据库
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • vuex 笔记整理
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 入口文件开始,分析Vue源码实现
  • 使用SAX解析XML
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 我有几个粽子,和一个故事
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • # 透过事物看本质的能力怎么培养?
  • ###STL(标准模板库)
  • #pragma once
  • #知识分享#笔记#学习方法
  • $L^p$ 调和函数恒为零
  • (1)Android开发优化---------UI优化
  • (152)时序收敛--->(02)时序收敛二
  • (2)从源码角度聊聊Jetpack Navigator的工作流程
  • (24)(24.1) FPV和仿真的机载OSD(三)
  • (C++哈希表01)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第14章泛型第2节(泛型类的类构造函数)
  • (k8s)Kubernetes本地存储接入
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (STM32笔记)九、RCC时钟树与时钟 第一部分
  • (二)构建dubbo分布式平台-平台功能导图
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (一)为什么要选择C++
  • ***php进行支付宝开发中return_url和notify_url的区别分析