单元测试工具 https://django-debug-toolbar.readthedocs.io/en/latest/installation.html
pip install django-debug-toolbar
INSTALLED_APPS = [
...
'debug_toolbar',
]
STATIC_URL = '/static/'
if settings.DEBUG:
import debug_toolbar
urlpatterns.append(path("__debug__/",include(debug_toolbar.urls)))
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware', #必须在最前面
...
]
#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 = {
}
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