Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django抛出模板Doesnotexist_Python_Django_Web_Backend - Fatal编程技术网

Python Django抛出模板Doesnotexist

Python Django抛出模板Doesnotexist,python,django,web,backend,Python,Django,Web,Backend,这不是我第一次遇到这个错误,但通常我会通过向template_DIRS元组添加一个模板路径来解决它,尽管这对我不再有效。以下是my settings.py模板片段: TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to

这不是我第一次遇到这个错误,但通常我会通过向template_DIRS元组添加一个模板路径来解决它,尽管这对我不再有效。以下是my settings.py模板片段:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
 os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),
)
我的已安装应用程序代码段:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
 #this is my app right here.
'boilerplate',
)
还有我在views.py文件中的简单代码:

from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
 return render_to_response('test.html',context_instance=RequestContext(request))

我相信重要的一点是,我的templates文件夹位于我的项目结构中,而不是我的应用程序中。有什么想法吗?

这是您需要实现的文件夹结构

myproject/
  ...
  myapp/
    views.py
    ...
    templates/
      myapp/
        mytemplate.html
这就是在views.py中执行的操作

def index(request):
 return render_to_response('myapp/test.html',context_instance=RequestContext(request))
你的模板没问题


阅读本文了解更多信息:

模板是否在项目目录中并不重要。打印
os.path.join(os.path.dirname(\uuuu file\uuuu),“…”,“templates”).replace(“\\”,“/”)
的结果是什么?另外,您是否厌倦了将绝对文件路径作为字符串放在模板元组中,以查看该操作是否只是尝试了第二个建议,但没有成功。错误屏幕向我显示以下消息:“/home/user/PyWeb/django_backbone/templates/test.html”@user1659653我不知道您应该在path@zinking实际上有一个空间。django_主干中有一个下划线,用户配置文件框将隐藏该下划线。尝试放大/复制itI我完全忘记了这个线程。是的,我有一个错误的文件夹结构,我解决了这个星期前,但这是正确的答案。谢谢你的发帖。