debug_toolbar 安装整理
时间:2009-08-16 来源:hkebao
一开始我看了一篇文章内容如下:
一、上http://packages.debian.org/zh-cn/sid/python-django-debug-toolbar去下载debug_toolbar。注意,虽然下载的不是zip或rar,但仍然可用解压缩软件打开(我用的是7-zip)。需要注意的是,下载的debug_toolbar非常小,才19k左右。要知道光一个jquery.js就快100k了,所以这个包是不完整的。下载页面还有一个libjs-jquery链接,将jquery也一并下载下来,否则debug_toolbar是用不了的。
二、将下载解压后的debug_toolbar加入到PYTHONPATH中去。咱们就统一放在$Python_HOME\Lib\site-packages下,和django它们在一起好管理。然后将解压后的jquery文件统统放在debug_toolbar\media文件夹下。
三、从这一步开始,就要开始修改django配置文件的参数了。打开settings.py,先确定你的确是以DEBUG模式运行“DEBUG = True”。 接着找到MIDDLEWARE_CLASSES配置项,将“debug_toolbar.middleware.DebugToolbarMiddleware,”加在Session,Auth和GZip等中间件后面。
四、接着在settings.py补上一句:INTERNAL_IPS = ('127.0.0.1',),网上说还需要在TEMPLATE_DIRS配置项中,将debug_toolbar的模板路径配置进去,像:
Python代码
- TEMPLATE_DIRS = (
- ......
- "D:/Python25/Lib/site-packages/debug_toolbar/templates",
- )
TEMPLATE_DIRS = ( ...... "D:/Python25/Lib/site-packages/debug_toolbar/templates", )
报的错误为:
Python代码
- Traceback (most recent call last):
- File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 278, in run
- self.result = application(self.environ, self.start_response)
- File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 635, in __call__
- return self.application(environ, start_response)
- File "D:\Python25\Lib\site-packages\django\core\handlers\wsgi.py", line 239, in __call__
- response = self.get_response(request)
- File "D:\Python25\Lib\site-packages\django\core\handlers\base.py", line 67, in get_response
- response = middleware_method(request)
- File "D:\Python25\lib\site-packages\debug_toolbar\middleware.py", line 53, in process_request
- self.debug_toolbar = DebugToolbar(request)
- File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 26, in __init__
- self.load_panels()
- File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 51, in load_panels
- raise exceptions.ImproperlyConfigured, 'Error importing debug panel %s: "%s"' % (panel_module, e)
- ImproperlyConfigured: Error importing debug panel debug_toolbar.panels.profiler: "No module named profiler"
Traceback (most recent call last): File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 278, in run self.result = application(self.environ, self.start_response) File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 635, in __call__ return self.application(environ, start_response) File "D:\Python25\Lib\site-packages\django\core\handlers\wsgi.py", line 239, in __call__ response = self.get_response(request) File "D:\Python25\Lib\site-packages\django\core\handlers\base.py", line 67, in get_response response = middleware_method(request) File "D:\Python25\lib\site-packages\debug_toolbar\middleware.py", line 53, in process_request self.debug_toolbar = DebugToolbar(request) File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 26, in __init__ self.load_panels() File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 51, in load_panels raise exceptions.ImproperlyConfigured, 'Error importing debug panel %s: "%s"' % (panel_module, e) ImproperlyConfigured: Error importing debug panel debug_toolbar.panels.profiler: "No module named profiler"
几经折腾后,发现那个配置中有两项是错误的,必须注释后,才可正常运行。难道我下载的debug_toolbar版本问题?正确的配置如下:
Python代码
- DEBUG_TOOLBAR_PANELS = (
- 'debug_toolbar.panels.sql.SQLDebugPanel',
- 'debug_toolbar.panels.headers.HeaderDebugPanel',
- 'debug_toolbar.panels.cache.CacheDebugPanel',
- #'debug_toolbar.panels.profiler.ProfilerDebugPanel',
- 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
- #'debug_toolbar.panels.templates.TemplatesDebugPanel',
- # If you are using the profiler panel you don't need the timer
- # 'debug_toolbar.panels.timer.TimerDebugPanel',
- )
DEBUG_TOOLBAR_PANELS = ( 'debug_toolbar.panels.sql.SQLDebugPanel', 'debug_toolbar.panels.headers.HeaderDebugPanel', 'debug_toolbar.panels.cache.CacheDebugPanel', #'debug_toolbar.panels.profiler.ProfilerDebugPanel', 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel', #'debug_toolbar.panels.templates.TemplatesDebugPanel', # If you are using the profiler panel you don't need the timer # 'debug_toolbar.panels.timer.TimerDebugPanel', )
经过一系列的配置,终于看到激动的界面了:
===========================================================================================================================
正当我准备单击“EXPLAN”,查看执行情况时,竟然没有任何反应。一查看控制台,又是404错误:
Python代码
- [07/Feb/2009 17:36:58] "GET /manager/list_catalogs HTTP/1.1" 200 7448
- [07/Feb/2009 17:36:58] "GET /__debug__/m/jquery.js HTTP/1.1" 304 0
- [07/Feb/2009 17:36:58] "GET /__debug__/m/toolbar.min.js HTTP/1.1" 304 0
- [07/Feb/2009 17:36:58] "GET /__debug__/m/toolbar.min.css HTTP/1.1" 304 0
- [07/Feb/2009 17:37:01] "POST /__debug__/sql_explain/?sql=SELECT%20COUNT%28%2A%29
- %20FROM%20%60catalogs_catalog%60¶ms=%5B%5D&time=0.00&hash=9ddba759e58f7a98f7
- be15347a7a6c6dbd48da51 HTTP/1.1" 404 2941
[07/Feb/2009 17:36:58] "GET /manager/list_catalogs HTTP/1.1" 200 7448 [07/Feb/2009 17:36:58] "GET /__debug__/m/jquery.js HTTP/1.1" 304 0 [07/Feb/2009 17:36:58] "GET /__debug__/m/toolbar.min.js HTTP/1.1" 304 0 [07/Feb/2009 17:36:58] "GET /__debug__/m/toolbar.min.css HTTP/1.1" 304 0 [07/Feb/2009 17:37:01] "POST /__debug__/sql_explain/?sql=SELECT%20COUNT%28%2A%29 %20FROM%20%60catalogs_catalog%60¶ms=%5B%5D&time=0.00&hash=9ddba759e58f7a98f7 be15347a7a6c6dbd48da51 HTTP/1.1" 404 2941
马上配置上,再次刷新(django修改配置文件不用重启的感觉太好了),再次点击“EXPLAN”,终于看到久违的页面:
总的来说,debug_toolbar还是款不错的小插件。先前,由于对django的orm不熟悉,看到分页代码总是类似于下面:
Python代码
- all_catalogs = Catalog.objects.all()
- paginator = Paginator(all_catalogs, 2)
- ......
all_catalogs = Catalog.objects.all() paginator = Paginator(all_catalogs, 2) ......
以为每次的“all()”调用是整个表都查一遍。后来根据debug_toolbar看了看SQL queries的输出,原来和所想的一样:两个sql语句,一个用来统计总数,一个用来取当前记录的。比如:
Python代码
- SELECT COUNT(*) FROM `catalogs_catalog`
- SELECT `catalogs_catalog`.`id`, `catalogs_catalog`.`name`, `catalogs_catalog`.`datetime` FROM `catalogs_catalog` LIMIT 2 OFFSET 6
SELECT COUNT(*) FROM `catalogs_catalog` SELECT `catalogs_catalog`.`id`, `catalogs_catalog`.`name`, `catalogs_catalog`.`datetime` FROM `catalogs_catalog` LIMIT 2 OFFSET 6
好了,更多python和django的魅力还得慢慢的去发掘......
(URL:http://www.javaeye.com/topic/327026)
我的整理:
我的下载地址:http://github.com/dcramer/django-debug-toolbar/tree/master
第二步:将下载下来的压缩包解压缩后 我是放到了site-package目录下面的
这样做的好处就在于我能够在 我的项目中直接安装进来这个工具!
第三步:
配置项目中的中间件:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware', (多了这一条哦)
)
第四步:添加
INTERNAL_IPS = ('127.0.0.1',) 这个表示说我们能够在这个URL地址下面使用的!
、 第五步:添加一个新配置内容如下:
DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.sql.SQLDebugPanel',
'debug_toolbar.panels.headers.HeaderDebugPanel',
'debug_toolbar.panels.cache.CacheDebugPanel',
#'debug_toolbar.panels.profiler.ProfilerDebugPanel',
'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
#'debug_toolbar.panels.templates.TemplatesDebugPanel',
# If you are using the profiler panel you don't need the timer
# 'debug_toolbar.panels.timer.TimerDebugPanel',
)
第六步:在我们的模板目录下面修改下
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__),"templates"),
"D:/Python25/Lib/site-packages/debug_toolbar/templates",
)
将它自带的模板引入进来!
第七步:在INSTALLED_APPS = ( 中将这个项目引入进来
'debug_toolbar',
上面我转载的没有写这个 所以就找不到模板了!得将这个加入进来才行的!