【问题标题】:Django: How to load a script for every page?Django:如何为每个页面加载脚本?
【发布时间】:2016-04-21 19:00:17
【问题描述】:

假设我想在我的视图中为每个页面包含引导程序或角度,有没有比将同一行复制到每个文件更优雅的方法?

【问题讨论】:

标签: javascript python html css django


【解决方案1】:

您需要django template inheritance。在模板base.html 中包含,并在其中定义将为子模板填充的块:

<html>
    <!-- base.html -->
    ......
    {% block content %}
    {% endblock %}
    <!-- You include your js/css here -->
    <script type="text/javascipt" src="{{ STATIC_URL }}jquery.js">
    <link rel="stylesheet" type="text/css" href="mystyle.css">
</html>

然后对于您扩展 base.html 并覆盖 block content 的所有模板,如下所示:

<!-- sub.html -->
{% extends "base.html" %}
{% block content %}
        <!-- You current page html goes here -->
{% endblock %}

这样,您在base.html 中包含的内容将被自动继承并在 sub.html 中可用。

【讨论】:

  • base.html 可以是开放式的吗?例如,它可以以没有
猜你喜欢
  • 2019-03-25
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多