【问题标题】:Django TemplateDoesNotExist ExceptionDjango TemplateDoesNotExist 异常
【发布时间】:2012-05-26 11:41:18
【问题描述】:

我试图使用 django-endless-pagination twitter pagination,但 Django 正在抛出异常,我无法调试它。

创建这些模板后:

search_results.html:

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

结果.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')

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

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

模板加载器事后分析

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/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 %}

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

【问题讨论】:

    标签: django templates exception


    【解决方案1】:

    在您的 search_results.html 页面中,您有 {% include page_template %},我假设您正在使用 locals() 将其传递到模板中。你能把它输出到模板上来验证page_template的值是多少吗?将 {{ page_template }} 替换为 {% include page_template %} 以验证其获取的 url 是否正确。

    【讨论】:

      【解决方案2】:

      我是Django 的新手,我将安装的应用程序方向放在“模板区域”并收到此错误。

      仔细检查为什么将路线放入模板中,
      模板和已安装的应用列表都以djang.something 开头,但它们是不同的。

      【讨论】:

        猜你喜欢
        • 2019-08-18
        • 2022-01-11
        • 2012-07-03
        • 2011-06-13
        • 2014-10-17
        • 1970-01-01
        • 2018-09-29
        • 1970-01-01
        • 2018-06-02
        相关资源
        最近更新 更多