当我尝试执行该函数时,我得到(TemplateDoesNotExist at /search/)我的模板文件夹位于 C:\Users\Rafik\Documents\myproject\env_mysite\Scripts\mysie\books\templates python 代码运行良好但它说 TemplateDoesNotExist
视图.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
从书籍导入视图
网址模式 = [
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',
],
},
},
]