Warm tip: This article is reproduced from serverfault.com, please click

templates-Django TemplateDoesNotExist异常

(templates - Django TemplateDoesNotExist Exception)

发布于 2012-05-18 22:16:21

我想使用django-endless-pagination twitter pagination,但是Django抛出异常,调试时遇到了麻烦。

创建这些模板后:

search_results.html:

<h2>Entries:</h2>
{% include page_template %}

results.html:

{% for object in objects %}
    {# your code to show the entry #}
{% endfor %}

我将它们添加到我的项目模板目录中。然后,我创建此视图:

def search(request):
    if 'q' in request.GET:  #Need to add input variable to html code
        q = request.GET['q']
        if q:
            stuff = Stuff.objects.filter(name__icontains=q)
            template = "search_results.html"
            page_template = "results.html"
            if request.is_ajax():
                template = page_template
            return render_to_response(template, locals())
    return render_to_response('search_page.html')

当我转到结果url时,我得到: 异常类型: TemplateDoesNotExist

异常位置: find_template的第138行中的/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py

模板加载器事后分析

Django尝试按以下顺序加载这些模板:使用加载器django.template.loaders.filesystem.Loader:/ Users / some_account / Dropbox / code / project / project / html(存在文件)/ Users / some_account / Dropbox / code / project / project / course_database / templates(存在文件)使用加载程序django.template.loaders.app_directories.Loader:/ Users / some_account / Dropbox / code / project / project / course_database / templates(存在文件)/Library/Python/2.7/2.7/site -packages / endless_pagination / templates(存在文件)/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/templates(存在文件)/ Library / Frameworks / Python .framework / Versions / 2.7 / lib / python2.7 / site-packages / django / contrib / admin / templates(存在文件)/Library/Frameworks/Python.framework/Versions/2.7/lib/python2。7 / site-packages / django / contrib / admindocs / templates(文件存在)

模板加载期间的错误位于以下行中: {% include page_template %}

为什么会出现此异常,我该如何解决?

Questioner
HighLife
Viewed
11
Furbeenator 2012-05-19 06:45:37

在你的search_results.html页面中,你有{% include page_template %},并且我假设你正在使用locals()将其传递到模板中。你可以将其输出到模板上以验证page_template值是多少吗?{{ page_template }}代替{% include page_template %}以验证它是否得到正确的URL。