python - Django 模板加载器行为? (得到 Django TemplateDoesNotExist 异常)

标签 python django django-templates

文件夹结构

我目前正在从 Django 书中学习 Django。我有以下目录结构:

.
└── mysite
    ├── books
    │   ├── __init__.py
    │   ├── models.py
    │   ├── templates
    │   │   ├── search_form.html
    │   │   └── search_results.html
    │   ├── tests.py
    │   └── views.py
    ├── contact
    │   ├── forms.py
    │   ├── __init__.py
    │   ├── templates
    │   │   └── contact_form.html
    │   └── views.py
    ├── manage.py
    └── mysite
        ├── __init__.py
        ├── settings.py
        ├── templates
        │   ├── base.html
        │   ├── current_datetime.html
        │   └── hours_ahead.html
        ├── urls.py
        ├── views.py
        └── wsgi.py

其中 books、contact 和 mysite 是文件夹。我还有一个单独的模板子文件夹,用于存储模板的每个文件夹。

故事

现在,在 /mysite/settings.py 中,我有以下几行:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/'),
)

/contact/views.py 中,我有以下行(注意这里的渲染来自 django.shortcuts):

return render(request, 'contact_form.html', {'form': form})

但是,上面的行给了我一个 TemplateDoesNotExist 异常。这是因为模板加载器在 /mysite/templates/ 而不是 /contact/templates/ 下查找文件 contact_form.html。

很公平,尽管我不明白为什么会这样。但是,我有另一种情况,它的行为有所不同。

回想上面的文件结构,我还有books/views.py。其中,我有以下几行(在不同的逻辑 block 中):

return render(request, 'search_results.html',
                {'books': books, 'query': q})

return render(request, 'search_form.html', {'errors': errors})

如果你回头看上面,search_results.html 和 search_form.html 在 /books/templates/ 文件夹下,而不是 /mysite/templates/ 文件夹下,然而模板加载器设法找到了那些文件。请注意,即使我将 search_results.html 和 search_form.html 移动到 /mysite/templates/ 中,它仍然有效。

所以我的问题是,为什么加载器查找模板的位置不同?

文件

无论如何,这是我的/contact/views.py 的样子:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.mail import send_mail
from forms import ContactForm
import os

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                    cd['subject'],
                    cd['message'],
                    cd.get('email', 'noreply@example.com'),
                    ['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
    print os.path.join(os.path.dirname(__file__), 'templates')
    return render(request, 'contact_form.html', {'form': form})

这是我的/books/views.py:

from django.shortcuts import render
from django.http import HttpResponse
from books.models import Book


def search(request):
    errors = []
    if 'q' in request.GET:
        q = request.GET['q']
        if not q:
            errors.append('Enter a search term.')
        elif len(q) > 20:
            errors.append('Please enter at most 20 characters.')
        else:
            books = Book.objects.filter(title__icontains=q)
            return render(request, 'search_results.html',
                {'books': books, 'query': q})
    return render(request, 'search_form.html', {'errors': errors})

最佳答案

由于您显然确实安装了应用程序目录模板加载器,并且由于它适用于您的 books 应用程序,所以我只能得出结论,您没有 contact settings.INSTALLED_APPS

关于python - Django 模板加载器行为? (得到 Django TemplateDoesNotExist 异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299415/

相关文章:

python - 如何在 Python 3 中覆盖 file.write()?

python - 我可以用键盘以某种方式中断 GridSearchCV 并且在此之前仍然收集最好的参数吗?

python - 从 Django 中的多对多关系中排除一行

django - request.method == "POST"在 Django 中意味着什么?

python - 为什么我会收到无效的 block 标签 : 'static' error?

python - python中面向对象的构造等价于什么?

python - 有什么方法可以使用 tweepy 检索推文事件

python - 如何使用 Python 字符串格式将表示美分的整数转换为表示美元的 float ?

django - sorl-thumbnail ImageField 动态 upload_to 路径

django - 当 View 需要参数时,表单操作参数中的内容是什么?