如何在Django 1.9中重置模板加载程序缓存?
我以前能够导入如何在Django 1.9中重置模板加载程序缓存?,django,django-templates,Django,Django Templates,我以前能够导入django.template.loader.template\u source\u loaders,并在所有加载程序上调用reset(),以重置所有模板加载程序缓存,但这不再有效 如何在Django 1.9中重置模板加载程序缓存 我的设置,以防有用: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.j
django.template.loader.template\u source\u loaders
,并在所有加载程序上调用reset()
,以重置所有模板加载程序缓存,但这不再有效
如何在Django 1.9中重置模板加载程序缓存
我的设置,以防有用:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'accounts/templates')],
'APP_DIRS': True
}
]
这是我加载模板的方式:
from django.template import TemplateDoesNotExist
from django.template.loader import get_template
from django.template.response import TemplateResponse
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import ensure_csrf_cookie
@never_cache
@ensure_csrf_cookie
def view(request, **kwargs):
try:
template = get_template('index.html')
except TemplateDoesNotExist:
return HttpResponse("Run `make template`")
return TemplateResponse(request, template)
我在本地开发人员使用内置的
runserver
,DEBUG=True时遇到了这个问题。此问题不适用于生产环境,因为模板将始终存在。Django不会缓存模板,但会缓存应用目录加载程序使用的应用模板目录列表。如果在启动服务器后创建一个新目录,例如polls/templates
,则Django将不会在该目录中拾取模板,直到服务器重新启动。非常简单:
from django.template.utils import get_app_template_dirs
get_app_template_dirs.cache_clear()
您似乎没有使用。我没有使用缓存的模板加载程序,但我注意到缓存:它缓存不存在的模板。然后,当我创建模板时,我必须重新启动服务器才能看到更改。@Blaise这太奇怪了。我在Django源代码中找不到这种缓存的任何证据。您是在服务器运行时创建模板目录,还是仅创建模板?问题是cached@Alasdair我正在删除并重新创建文件夹,这确实是个问题。现在,当我重新创建
index.html
时,我保留了这个文件夹,现在它可以正常工作了。谢谢大家的帮助!