Random Django TemplateDoesNotExist Errors
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/m开发者_如何转开发y_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
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.
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
精彩评论