【发布时间】:2018-07-17 14:41:26
【问题描述】:
当我尝试执行该函数时,我得到 (TemplateDoesNotExist at /search/) 我的模板文件夹位于 C:\Users\Rafik\Documents\myproject\env_mysite\Scripts\mysie\books\templates python 代码运行良好,但显示 TemplateDoesNotExist
views.py:
def 搜索(请求):
error = False
if 'q' in request.GET:
q = request.GET['q']
if not q:
error = True
elif len(q) > 20:
error = True
else:
books = Book.objects.filter(title__icontains=q)
return render(request, 'search_results.html', {'books': books, 'query': q})
return render(request, 'search_form.html', {'error': error})
应用程序/urls.py:
从 django.conf.urls 导入 url
从书籍导入视图
urlpatterns = [
url(r'^search-form/$', views.search_form),
url(r'^search/$', views.search)
]
模板设置.py
模板 = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
【问题讨论】:
-
请提供更多信息,视图是什么(CBV 或通用视图),settings.py 中的相关部分以及您的文件夹结构。
-
请查看tour,并阅读HELP center,然后阅读How to 、What types of questions should I avoid asking?并提供MCVE : Minimal, Complete, and Verifiable Example。如果周围的人可以轻松阅读并理解您的意思或问题所在,他们将更愿意提供帮助:)
标签: python-3.x django-2.0