5

我的问题最初表现为相当普通的“超出最大递归深度”错误ListView。在四处挖掘之后,我发现每次尝试加载相应页面时都有大量来自 dbtemplates 的缓存命中。这促使我编写自己的模板加载器,此时很明显,load_template_source每次加载页面时 Django 都调用了 69 次。

为什么Django会load_template_source多次调用?

访客索引(数据库模板)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>{{ site.name }}</title>
    </head>
    <body id="home">
    <pre>
        Starting
        {% for video in videos %}
            {{ video.headline }}
        {% endfor %}
    </pre>
    </body>
</html>

网址.py

from django.conf.urls import include, url
from django.conf.urls.defaults import patterns
from videos.views import *

urlpatterns = patterns('',
    url(r'^$', VisitorIndex.as_view(), name='index'),
)

视图.py

import logging
from django.views.generic import ListView
from videos.models import Video

logger = logging.getLogger(__name__)

class VisitorIndex(ListView):
    context_object_name = 'videos'
    template_name = 'visitor_index'

    def get_queryset(self):
        logger.debug('VisitorIndex.get_queryset()')
        return Video.on_site.filter(privacy_mode='PUB')

加载器.py

import logging
from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader
from dbtemplates.models import Template

logger = logging.getLogger(__name__)

class Loader(BaseLoader):
    is_usable = True

    def load_template_source(self, template_name, template_dirs=None):
        try:
            logger.debug('Loader.load_template_source(%s, %s)' % (template_name, template_dirs))
            template = Template.on_site.get(slug__exact=template_name).template_body
            logger.debug('Loaded template.')
            return template, template_name
        except Template.DoesNotExist:
            logger.debug('Template.DoesNotExist caught.')
            raise TemplateDoesNotExist, template_name

模型.py

import logging
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.db import models

logger = logging.getLogger('__name__')

class Template(models.Model):
    slug = models.CharField(max_length=80)
    template_body = models.TextField()
    site = models.ForeignKey(Site)

    objects = models.Manager()
    on_site = CurrentSiteManager('site')

    def __unicode__(self):
        return self.slug

设置.py

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'dbtemplates.loader.Loader',
)

app.log(使用跟踪更新)

2013-05-09 19:15:43,634 [DEBUG] dynamicsites.middleware: ENV_HOSTNAMES lookup subdomain=None domain=mydomain.com domain_unsplit=mydomain.com
2013-05-09 19:15:43,634 [DEBUG] dynamicsites.middleware: Checking database for domain=mydomain.com
2013-05-09 19:15:43,679 [DEBUG] dynamicsites.middleware: Using site id=2 domain=mydomain.com
2013-05-09 19:15:43,714 [DEBUG] videos.views: VisitorIndex.get_queryset()
2013-05-09 19:15:43,716 [DEBUG] django.template.response: SimpleTemplateResponse.rendered_content()
2013-05-09 19:15:43,717 [DEBUG] django.template.response: Start self.resolve_template()
2013-05-09 19:15:43,717 [DEBUG] django.template.response: SimpleTemplateResponse.resolve_template().  Found list or tuple.
2013-05-09 19:15:43,717 [DEBUG] django.template.response: Calling loader.select_template(template=['visitor_index', u'videos/video_list.html'])
2013-05-09 19:15:43,720 [DEBUG] dbtemplates.loader: Loader.load_template_source(visitor_index, None)
2013-05-09 19:15:43,722 [DEBUG] dbtemplates.loader:   File "/usr/lib/python2.7/threading.py", line 525, in __bootstrap
    self.__bootstrap_inner()
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
    handler.run(self.server.get_app())
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
    return self.application(environ, start_response)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
    response = response.render()
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/response.py", line 121, in render
    self.content = self.rendered_content
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/response.py", line 91, in rendered_content
    template = self.resolve_template(self.template_name)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/response.py", line 60, in resolve_template
    t = loader.select_template(template)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 188, in select_template
    return get_template(template_name)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 146, in get_template
    template, origin = find_template(template_name)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 135, in find_template
    source, display_name = loader(name, dirs)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 43, in __call__
    return self.load_template(template_name, template_dirs)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 46, in load_template
    source, display_name = self.load_template_source(template_name, template_dirs)
  File "/home/wesley/howl/dbtemplates/loader.py", line 19, in load_template_source
    logger.debug(''.join(traceback.format_stack()))

2013-05-09 19:15:43,885 [DEBUG] dbtemplates.loader: Loaded template.
2013-05-09 19:15:43,886 [DEBUG] django.template.response: Done.
2013-05-09 19:15:43,887 [DEBUG] django.template.response: Done.  <django.template.base.Template object at 0x7fbdf8215050>
2013-05-09 19:15:43,887 [DEBUG] django.template.response: Start self.resolve_context()
2013-05-09 19:15:43,920 [DEBUG] django.template.response: Start template.render(context)
2013-05-09 19:15:43,920 [DEBUG] django.template.base: Template.render(context)
2013-05-09 19:15:43,920 [DEBUG] django.template.base:     context.render_context.push()
2013-05-09 19:15:43,920 [DEBUG] django.template.base:     Done.
2013-05-09 19:15:43,921 [DEBUG] django.template.base: Try self._render(context)
2013-05-09 19:15:43,932 [DEBUG] dbtemplates.loader: Loader.load_template_source(visitor_index, None)
2013-05-09 19:15:43,933 [DEBUG] dbtemplates.loader:   File "/usr/lib/python2.7/threading.py", line 525, in __bootstrap
    self.__bootstrap_inner()
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
    handler.run(self.server.get_app())
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
    return self.application(environ, start_response)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
    response = response.render()
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/response.py", line 121, in render
    self.content = self.rendered_content
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/response.py", line 97, in rendered_content
    content = template.render(context)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/base.py", line 146, in render
    return self._render(context)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/test/utils.py", line 65, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render
    bit = self.render_node(node, context)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 148, in render
    len_values = len(values)
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/db/models/query.py", line 90, in __len__
    self._result_cache = list(self.iterator())
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/db/models/query.py", line 301, in iterator
    for row in compiler.results_iter():
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/home/wesley/howl/debug_toolbar/utils/tracking/db.py", line 134, in execute
    template_info = get_template_info(node.source)
  File "/home/wesley/howl/debug_toolbar/utils/__init__.py", line 80, in get_template_info
    template_source = origin.reload()
  File "/home/wesley/environments/howl/local/lib/python2.7/site-packages/django/template/loader.py", line 80, in reload
    return self.loader(self.loadname, self.dirs)[0]
  File "/home/wesley/howl/dbtemplates/loader.py", line 19, in load_template_source
    logger.debug(''.join(traceback.format_stack()))

[...]
4

1 回答 1

4

没有任何问题地复制了您的项目。

Loader 只被调用一次,没有任何递归。我还添加了一些简单的必要文件:videos/models.py 以及videos/admin.py、dbtemplates/admin.py,以便添加两条视频记录和一个循环播放视频的记录模板:{% for video in videos %}...{% endfor %}

我仍然无法相信递归是可能的Loader.load_template_source()。需要更多信息。尝试通过添加以下两行来从 Loader 的每次调用中获取回溯:

...
logger.debug('Loader.load_template_source......')
import traceback                                  # added
logger.debug(''.join(traceback.format_stack()))
...

(我担心循环或递归源于其他代码,这些代码产生了许多线程,这些线程都在数据库操作之前等待,直到当前运行的操作完成。)


编辑问题是由django-debug-toolbar引起的。

  • 数据库游标由 debug_toolbar 监控代码包裹。
  • 它尝试自省堆栈中template_inforender.
  • Template_info 使用模板重新加载。
  • 这又需要访问数据库。

您可以禁用与数据库或模板相关的调试工具栏的某些部分(未经测试是否有帮助)或完全禁用它。数据库操作由 Django 本身很好地监控,而调试工具栏所做的同样工作现在不那么重要了。

于 2013-05-09T23:00:00.510 回答