服务器不遵循模板目录路径

2024-06-28 20:15:48 发布

您现在位置:Python中文网/ 问答频道 /正文

TEMPLATE_DIRS中“我的模板”文件夹的路径如下所示:

os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + '/static/templates/'

当我在本地运行服务器并打开页面时,一切正常,它会在~/Documents/projects/clupus/static/templates检测模板。每当我将所有内容拉到服务器上并访问URL时,都会出现以下错误:

Django tried loading these templates, in this order: Using loader django.template.loaders.filesystem.Loader: /home/ubuntu/public_html/clupus.com/clupus/templates/clupus/index.html (File does not exist)

它没有跟随TEMPLATE_DIRS,并且正在错误的目录中查找。我检查了服务器上的TEMPLATE_DIRS值,它与本地的值匹配。有什么问题吗?你知道吗

编辑

令人尴尬的是,我的代码没有任何问题,我只是忘记了通过执行sudo service apache2 restart重新启动apache。至于为什么我的模板文件夹是静态的,这是在前端开发人员的要求。当我问他为什么说:

the reason why they are inside it is because I'm trying to reference the templates in Javascript aswell because we are using shared templates between server and client


Tags: thepathin服务器文件夹模板oshtml
2条回答

第一件事:静态和模板是两个非常独立的“东西”,不应该有任何共同点-这意味着“模板”目录应该在“静态”旁边,而不是在它里面。你知道吗

默认情况下,Django查找模板文件的位置有两个:

  • TEMPLATE_DIRS中指定的目录
  • templates目录(位于INSTALLED_APPS中的应用程序)

您的设置似乎对第一种情况有效,因此可能是一些简单的错误:

  • TEMPLATE_DIRS仍然是元组吗?TEMPLATE_DIRS =( os.path.blah.blah, ),逗号是必需的
  • templates目录在static目录中时,我看不到一种确切的机制会导致问题,但是您应该将它从那里移出。你知道吗
  • 确保TEMPLATE_LOADERS设置其default value

静态url应该指向staticfiles目录。为什么要把模板放在静态文件下?你可以把它作为一个单独的文件夹放在主文件夹中(连同管理.py)你知道吗

相关问题 更多 >