【发布时间】:2014-12-23 14:51:47
【问题描述】:
我无法在 Django 1.7 中呈现任何 html 页面。我的 'index.html' 在 'project/seatalloc/templates/index.html' 中,而我在 project/seatalloc/views.py 中的 view.py 看起来像:
def index(request):
return render(request, 'index.html', dirs=('templates',))
project/project/settings.py 已设置模板目录:
TEMPLATE_DIRS = (
'/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates',
)
urls.py:
urlpatterns = patterns('',
url(r'^seatalloc/', include('seatalloc.urls')),
url(r'^admin/', include(admin.site.urls)),
)
我已尝试严格遵循文档,但无法弄清楚 Django 是否检测到该文件,为什么我在 /seatalloc/ 处收到 TemplateDoesNotExist 错误。我是 Django 新手,有人可以帮忙吗?
【问题讨论】:
-
看起来
setalloc是一个应用程序。您实际上不需要告诉template_dirs它的模板,正如您在突出显示的行下方看到的那样,应用程序目录加载器已经找到了它。template_dirs用于非应用模板,通常位于项目根目录中,而不是在应用中。 -
我发现了问题:我之前提供的 url 是 url(r'^$', views.index, name='index'),我把它改成了 url(r'^index $', views.index, name='index'), this + 将 templates_dir 移动到项目根目录解决了它。感谢大家的帮助
标签: python django django-templates