随机 Django TemplateDoesNotExist 错误

发布于 2024-10-24 01:56:01 字数 2903 浏览 1 评论 0原文

我有一个正在生产中的 Django 站点,使用 Python 2.7/mod_wsgi 3.3 运行 Django 1.2.5。在大多数情况下,一切都运行良好,但该网站似乎完全随机地抛出错误。它们都以以下形式结尾:

TemplateDoesNotExist: xxx

这些模板肯定存在,当我输入导致异常的 URL 时,该 URL 似乎总是有效。但是,大约每 30 次页面浏览就会引发一次此错误。

我发现这篇关于类似问题的文章: http://leequerv.blogspot.com/2009/11/re-settingspy-seems-to-be-cached-or_24.html但我只运行一个Django应用程序,所以它似乎并不直接适用。

我正在使用一些子域中间件,它根据我网站的子域交换模板目录(即,如果没有子域,则使用常规模板目录;如果有 m.xxx 子域,则使用我的移动模板目录)。除了这些零星的生产服务器错误之外,所有这些在我的开发服务器和生产服务器上都运行良好。当很多人使用该网站时,这是否可能会造成竞争条件?

你有什么想法可能导致它或我应该从哪里开始寻找?

编辑:

这是切换模板目录的中间件代码部分:

    subdomain = getattr(request, 'subdomain', False)

    if subdomain is not False:
        try:
            request.urlconf = settings.SUBDOMAIN_URLCONFS[subdomain]
        except KeyError:
            pass
        try:
            settings.TEMPLATE_DIRS = settings.SUBDOMAIN_TEMPLATE_DIRS[subdomain]
        except KeyError:
            pass

这是我的设置文件中保存模板目录信息的部分:

SUBDOMAIN_URLCONFS = { 无:'my_site.urls', 'm': 'mobile.urls' }

JQM_TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, "templates/mobile/"),
)

SUBDOMAIN_TEMPLATE_DIRS = {
    None: TEMPLATE_DIRS,
    'm': JQM_TEMPLATE_DIRS
}

编辑#2

这是我的回溯:

Traceback (most recent call last):

 File "/home/my_username/webapps/my_site/lib/python2.7/django/core/handlers/base.py", line 100, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/home/my_username/python-environments/my_site/lib/python2.7/site-packages/endless_pagination/decorators.py", line 55, in decorated
   return view(request, *args, **kwargs)

 File "/home/my_username/webapps/my_site/my_site/local_apps/team/views.py", line 68, in team_detail
   return render_to_response(template, context, context_instance=RequestContext(request))

 File "/home/my_username/webapps/my_site/lib/python2.7/django/shortcuts/__init__.py", line 20, in render_to_response
   return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 181, in render_to_string
   t = get_template(template_name)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 157, in get_template
   template, origin = find_template(template_name)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 138, in find_template
   raise TemplateDoesNotExist(name)

TemplateDoesNotExist: team_detail.html

I have a Django site in production running Django 1.2.5 using Python 2.7/mod_wsgi 3.3. For the most part everything works great, but it seems that the site is throwing errors totally at random. They all end in the form:

TemplateDoesNotExist: xxx

These templates definitely do exist, and when I type in the URL that caused the exception the URL always seems to work. However, once in approximately every 30 page views, this error is thrown.

I found this post about a similar problem: http://leequerv.blogspot.com/2009/11/re-settingspy-seems-to-be-cached-or_24.html but I am only running one Django application so it doesn't seem to apply directly.

I am using some subdomain middleware that swaps template directories depending on the subdomain of my site (i.e. it uses the regular template dirs if there is no subdomain, and uses my mobile template dirs if it has an m.xxx subdomain). All of this works great, both on my dev server and on the production server, except for these sporadic production server errors. Is it possible this is creating a race condition when lots of people are using the site?

Do you have any ideas what could be causing it or where I should begin looking?

Edit:

Here is the part of the middleware code where the template directories are switched:

    subdomain = getattr(request, 'subdomain', False)

    if subdomain is not False:
        try:
            request.urlconf = settings.SUBDOMAIN_URLCONFS[subdomain]
        except KeyError:
            pass
        try:
            settings.TEMPLATE_DIRS = settings.SUBDOMAIN_TEMPLATE_DIRS[subdomain]
        except KeyError:
            pass

Here is the part of my settings file that holds the template dir info:


SUBDOMAIN_URLCONFS = {
None: 'my_site.urls',
'm': 'mobile.urls'
}

JQM_TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, "templates/mobile/"),
)

SUBDOMAIN_TEMPLATE_DIRS = {
    None: TEMPLATE_DIRS,
    'm': JQM_TEMPLATE_DIRS
}

Edit #2:

Here is my traceback:

Traceback (most recent call last):

 File "/home/my_username/webapps/my_site/lib/python2.7/django/core/handlers/base.py", line 100, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/home/my_username/python-environments/my_site/lib/python2.7/site-packages/endless_pagination/decorators.py", line 55, in decorated
   return view(request, *args, **kwargs)

 File "/home/my_username/webapps/my_site/my_site/local_apps/team/views.py", line 68, in team_detail
   return render_to_response(template, context, context_instance=RequestContext(request))

 File "/home/my_username/webapps/my_site/lib/python2.7/django/shortcuts/__init__.py", line 20, in render_to_response
   return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 181, in render_to_string
   t = get_template(template_name)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 157, in get_template
   template, origin = find_template(template_name)

 File "/home/my_username/webapps/my_site/lib/python2.7/django/template/loader.py", line 138, in find_template
   raise TemplateDoesNotExist(name)

TemplateDoesNotExist: team_detail.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2024-10-31 01:56:01

为了解决这个问题,我删除了涉及交换模板目录路径的所有逻辑。我的猜测是,当系统的 python 路径发生更改时,其他并发请求有时会看到错误的 python 路径集。

因此,我只是为移动站点和常规站点提供了对模板目录的相同路径的访问权限,并确保不存在同名的冲突模板。

To remedy this problem, I removed all of the logic that involved swapping the paths to the template directories. My guess is that when the python paths were getting changed for the system, other concurrent requests were sometimes seeing the wrong set of python paths.

Thus, I just gave the mobile site and the regular site access to the same paths to the template directories and made sure there were no conflicting templates with the same name.

御弟哥哥 2024-10-31 01:56:01

就我而言,问题是 uwsgi 配置(uwsgi.ini)。仍然不知道为什么,但希望能帮助别人。

添加以下参数解决了我的问题:

disable-logging = True

max-worker-lifetime = 600

In my case the problem was the uwsgi configuration (uwsgi.ini). Still don't know why but hopefully will help someone else.

Adding the following parameters fixed my problem:

disable-logging = True

max-worker-lifetime = 600
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文