【问题标题】:Django static file inheritanceDjango静态文件继承
【发布时间】:2013-10-17 20:43:29
【问题描述】:

也许我是用 WordPress 的心态来解决我的问题,但我想要实现的是 Django 项目的父/子主题的概念。我想我可以通过定义两个模板目录来解决模板问题

TEMPLATE_DIRS = (
  '/home/Username/webapps/django/child/templates',
  '/home/Username/webapps/django/parent/templates',
)

但是有没有办法通过静态文件实现这一点?例如,我们向父级添加新功能,更新父级应用程序,该应用程序更新模板以及添加一些新的 javascript、LESS 和图像。

【问题讨论】:

    标签: django static django-staticfiles


    【解决方案1】:

    您不需要指定两个模板目录。有父模板和子模板的概念。所有子模板都扩展了父模板:

    base.html(我们经常使用这个名字作为父级)

    <html>
        <head>
           <!-- Some css, js stuff goes here-->
           {% block extra_head %}
               <!-- A block where child templates add more css and js stuff if needed -->
           {% endblock extra_head %}
        </head>
        <body>
            {% block body %}
               <!-- body content here -->
            {% endblock body %}
        </body>
    </html>
    

    然后子模板会将base.html扩展为:

    child.html

    {% extends "base.html" %}
    
    {% block body %}
        <h1>Hello!</h1>
    {% endblock body %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2012-06-21
      • 2012-12-02
      • 2015-03-20
      • 2015-06-10
      相关资源
      最近更新 更多