目录
- 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 = True
ALLOWED_HOSTS = []
INTERNAL_IPS=[
"127.0.0.1",
]
# Application definition
INSTALLED_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/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_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 = True
LANGUAGES = [
('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-field
DEFAULT_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节 添加第三方包