您的位置:首页 > 编程语言 > Go语言

django-debug-toolbar

2019-08-18 22:49 531 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/zhou_xiong1130/article/details/99710815

django-debug-toolbar

单元测试工具 https://django-debug-toolbar.readthedocs.io/en/latest/installation.html

安装 切换项目所在的虚拟环境

pip install  django-debug-toolbar

settings.py

INSTALLED_APPS = [
...
'debug_toolbar',

]

STATIC_URL = '/static/'

在主 urls.py中配置url

if settings.DEBUG:
import debug_toolbar
urlpatterns.append(path("__debug__/",include(debug_toolbar.urls)))

中间件

MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware', #必须在最前面
...
]

settings.py中做如下配置

#django-debug-toolbar

INTERNAL_IPS  = ['127.0.0.1']
# Application definition
DEBUG_TOOLBAR_PANELS = [
# 代表是哪个django版本
'debug_toolbar.panels.versions.VersionsPanel',
# 用来计时的,判断加载当前页面总共花的时间
'debug_toolbar.panels.timer.TimerPanel',
# 读取django中的配置信息
'debug_toolbar.panels.settings.SettingsPanel',
# 看到当前请求头和响应头信息
'debug_toolbar.panels.headers.HeadersPanel',
# 当前请求的想信息(视图函数,Cookie信息,Session信息等)
'debug_toolbar.panels.request.RequestPanel',
# 查看SQL语句
'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',
]

DEBUG_TOOLBAR_CONFIG = {

}

优化sql查询

def index(request):
count = settings.ONE_PAGE_NEWS_COUNT
#newses = News.objects.order_by('-pub_time')[0:count]
# newses = News.objects.all()[0:count]  如果按照这个  来讲  每页两篇文章
一篇文章 多两次外键查询  查询 类别  还有 作者  多篇文章  就乘以二
#提前一次外键查询不至于模板中进行 多余查询
#select_related('category','author') 就是进行 外键的查询 每个页面只需要执行一次即可
newses = News.objects.select_related('category','author').all()[0:count]
categories = NewsCategory.objects.all()
context = {
'newses': newses,
'categories': categories,
}
return render(request, 'news/index.html', context=context)

def news_detail(request,news_id):
try:
#news = News.objects.get(pk=news_id)
#news = News.objects.select_related('category','author').get(pk=news_id)
news = News.objects.select_related('category','author').prefetch_related('comments__author').get(pk=news_id)
#prefetch_related('comments__author') 注意这里是两个下划线
context = {
'news':news
}
return render(request, 'news/news_detail.html', context=context)
except News.DoesNotExist:
raise Http404
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: