我有djnago应用结构类似我如何可以包括从静态文件夹我的模板在Django
myproject/myapp
myproject/myapp/static/site/app/index.html
我想在我的文件中像这样
该模板url(r'^public$', TemplateView.as_view(template_name="/static/site/app/index.html")),
但它说没有找到模板
我有djnago应用结构类似我如何可以包括从静态文件夹我的模板在Django
myproject/myapp
myproject/myapp/static/site/app/index.html
我想在我的文件中像这样
该模板url(r'^public$', TemplateView.as_view(template_name="/static/site/app/index.html")),
但它说没有找到模板
import os
import sys
PORJECT_ROOT = os.path.join(os.path.dirname(__file__),'..')
sys.path.insert(0, os.path.join(PROJECT_ROOT,'static'))
确保文件夹包含在TEMPLATE_DIRS
设置中(位于settings.py中)。
一般(按照约定)模板文件夹是project_root_folder \ Templates文件夹,更新TEMPLATE_DIRS在settings.py:
import os
PORJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PORJECT_ROOT, 'templates'),
)
虽然静态商店只有JavaScript/CSS /图像fiels。
这将使未来的开发和部署更容易。
默认情况下,django会在app/templates目录中检查模板匹配,或者在project_root/templates /中检查。您应该像其他人所说的那样,在TEMPLATE_DIRS设置中列出目录。
我该如何硬编码那里的路径。像'myproject.static'或其他东西 – user1865341 2013-03-15 03:06:02
硬代码?对不起,我不明白你想做什么。首先,'template_name'出现错误。它应该是“static/site/app/index.html”,而不是“/static/site/app/index.html”,因为它被解释为来自'TEMPLATE_DIRS'中的其他路径的relativa路径。因此,添加保存文件夹static(e:“C:/ myprject/myapp /”或“/ home/django/myproject/myapp /”)的绝对路径。 – sanfilippopablo 2013-03-15 03:17:25