Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26278826
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2009-08-16 08:52:01

我的环境是:WIN2003+Python 2.5 + Django 1.0
一开始我看了一篇文章内容如下:

一、去下载debug_toolbar。注意,虽然下载的不是ziprar,但仍然可用解压缩软件打开(我用的是7-zip)。需要注意的是,下载的debug_toolbar非常小,才19k左右。要知道光一个jquery.js就快100k了,所以这个包是不完整的。下载页面还有一个链接,将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,”加在SessionAuthGZip等中间件后面。

 

四、接着在settings.py补上一句:INTERNAL_IPS = ('127.0.0.1',),网上说还需要在TEMPLATE_DIRS配置项中,将debug_toolbar的模板路径配置进去,像:

 

Python代码
  1. TEMPLATE_DIRS = (  
  2.   ......  
  3.     "D:/Python25/Lib/site-packages/debug_toolbar/templates",  
  4. )  

 

报的错误为:

 

 

Python代码
  1. Traceback (most recent call last):  
  2.   
  3.   File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 278in run  
  4.     self.result = application(self.environ, self.start_response)  
  5.   
  6.   File "D:\Python25\Lib\site-packages\django\core\servers\basehttp.py", line 635in __call__  
  7.     return self.application(environ, start_response)  
  8.   
  9.   File "D:\Python25\Lib\site-packages\django\core\handlers\wsgi.py", line 239in __call__  
  10.     response = self.get_response(request)  
  11.   
  12.   File "D:\Python25\Lib\site-packages\django\core\handlers\base.py", line 67in get_response  
  13.     response = middleware_method(request)  
  14.   
  15.   File "D:\Python25\lib\site-packages\debug_toolbar\middleware.py", line 53in process_request  
  16.     self.debug_toolbar = DebugToolbar(request)  
  17.   
  18.   File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 26in __init__  
  19.     self.load_panels()  
  20.   
  21.   File "D:\Python25\lib\site-packages\debug_toolbar\toolbar\loader.py", line 51in load_panels  
  22.     raise exceptions.ImproperlyConfigured, 'Error importing debug panel %s: "%s"' % (panel_module, e)  
  23.   
  24. ImproperlyConfigured: Error importing debug panel debug_toolbar.panels.profiler: "No module named profiler"  

 

几经折腾后,发现那个配置中有两项是错误的,必须注释后,才可正常运行。难道我下载的debug_toolbar版本问题?正确的配置如下:

 

 

Python代码
  1. DEBUG_TOOLBAR_PANELS = (  
  2.     'debug_toolbar.panels.sql.SQLDebugPanel',  
  3.     'debug_toolbar.panels.headers.HeaderDebugPanel',  
  4.     'debug_toolbar.panels.cache.CacheDebugPanel',  
  5.     #'debug_toolbar.panels.profiler.ProfilerDebugPanel',  
  6.     'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',  
  7.     #'debug_toolbar.panels.templates.TemplatesDebugPanel',  
  8.     # If you are using the profiler panel you don't need the timer  
  9.     # 'debug_toolbar.panels.timer.TimerDebugPanel',  
  10. )  

 

 

经过一系列的配置,终于看到激动的界面了:

 

 

 

===========================================================================================================================

 

 

 

正当我准备单击“EXPLAN”,查看执行情况时,竟然没有任何反应。一查看控制台,又是404错误:

 

Python代码
  1. [07/Feb/2009 17:36:58"GET /manager/list_catalogs HTTP/1.1" 200 7448  
  2. [07/Feb/2009 17:36:58"GET /__debug__/m/jquery.js HTTP/1.1" 304 0  
  3. [07/Feb/2009 17:36:58"GET /__debug__/m/toolbar.min.js HTTP/1.1" 304 0  
  4. [07/Feb/2009 17:36:58"GET /__debug__/m/toolbar.min.css HTTP/1.1" 304 0  
  5. [07/Feb/2009 17:37:01] "POST /__debug__/sql_explain/?sql=SELECT%20COUNT%28%2A%29  
  6. %20FROM%20%60catalogs_catalog%60¶ms=%5B%5D&time=0.00&hash=9ddba759e58f7a98f7  
  7. be15347a7a6c6dbd48da51 HTTP/1.1404 2941  

 

马上配置上,再次刷新(django修改配置文件不用重启的感觉太好了),再次点击“EXPLAN”,终于看到久违的页面:

 

 

 

 

 

总的来说,debug_toolbar还是款不错的小插件。先前,由于对djangoorm不熟悉,看到分页代码总是类似于下面:

 

 

Python代码
  1. all_catalogs = Catalog.objects.all()  
  2. paginator = Paginator(all_catalogs, 2)  
  3. ......  

 

以为每次的“all()”调用是整个表都查一遍。后来根据debug_toolbar看了看SQL queries的输出,原来和所想的一样:两个sql语句,一个用来统计总数,一个用来取当前记录的。比如:

 

 

Python代码
  1. SELECT COUNT(*) FROM `catalogs_catalog`  
  2. SELECT `catalogs_catalog`.`id`, `catalogs_catalog`.`name`, `catalogs_catalog`.`datetime` FROM `catalogs_catalog` LIMIT 2 OFFSET 6  

 

好了,更多pythondjango的魅力还得慢慢的去发掘......


(URL:)





我的整理:


    我的下载地址:

    第二步:将下载下来的压缩包解压缩后 我是放到了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',



上面我转载的没有写这个 所以就找不到模板了!得将这个加入进来才行的!







阅读(1097) | 评论(0) | 转发(0) |
0

上一篇:Django查看SQL语句

下一篇:Django资源列表

给主人留下些什么吧!~~