【问题标题】:Where to place my base.html file?在哪里放置我的 base.html 文件?
【发布时间】:2014-11-06 18:02:54
【问题描述】:

我在使用 base.html 文件时尝试关注Django's Template system,但是我不确定将 base.html 放在我的项目中的哪个位置。考虑到命名空间,Django 建议您的模板进入您的 'project_name/app_name/templates/app_name/' 目录。由于我想在项目中的多个应用程序中扩展我的 base.html,我的 base.html 应该放在哪里?

【问题讨论】:

    标签: html django django-templates


    【解决方案1】:

    由于这是一个共享模板,请将其放在项目的templates 目录中:

    project_name/templates/base.html
    

    另见:

    【讨论】:

    • 在尝试扩展 base.html 时收到“TemplateDoesNotExist at /news/”。我已更新我的 TEMPLATE_DIRS 设置以包括“/project_name/templates”和“/news/templates”。我的模板加载器后期状态 /project_name/templates/base.html (文件不存在) /news/templates/base.html (文件不存在)。我觉得我错过了一步,但似乎无法弄清楚。你对我可能做错了什么有什么建议吗?我是否必须调整我的“扩展”语句?
    • @Jimi 将templates 目录的绝对路径添加到TEMPLATE_DIRS 设置中,然后只使用{% extends 'base.html' %}。应该工作。
    • 我没有足够的代表来投票给您的答案,但非常感谢您的及时帮助。绝对路径解决了我收到的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多