【问题标题】:Trying to load a css stylesheet from a child template in django尝试从 django 中的子模板加载 css 样式表
【发布时间】:2020-11-17 20:10:04
【问题描述】:

我有一个包含此代码的父模板

<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}</title>
    {% load static %}
    <link rel="stylesheet" href="{% static 'base.css' %}"/>
    {% block additionalStyles %}{% endblock %}
</head>

我有一个包含此代码的子模板

{% extends 'base.html' %}
{% load static %}
{% block additionalStyles %}<link rel="sytlesheet" href="{% static 'rants.css' %}"/>{% endblock %}

这似乎不起作用,我也没有收到错误。 base.css 加载正常,当我检查网页上的元素时,我可以在 HTML 代码中看到&lt;link rel="sytlesheet" href="static/rants.css"/&gt;,但它对它应该的任何元素都没有任何影响。有没有更好的方法来做到这一点?为什么它不起作用?

编辑:它神奇地开始工作了?? idk 为什么或如何,如果有人有同样的问题,这里是新代码,在孩子:

{% extends "base.html" %}
{% load static %}
{% block title %}Rants{% endblock %} 
{% block additionalStyles %}
<link rel="stylesheet" href="{% static 'rants.css' %}"/>
{% endblock %}

在父母中:

    {% load static %}
    <title>{% block title %}{% endblock %}  </title>
    <link rel="stylesheet" href="{% static 'base.css' %}"/>
    {% block additionalStyles %} {% endblock %}

【问题讨论】:

  • 你添加{% load static %}标签了吗?
  • 是的,我在父模板和子模板中都做了

标签: python-3.x django django-templates


【解决方案1】:
  1. 请检查包含 CSS 文件的文件夹的路径。

  2. 当一切正常时,请记得清除浏览器缓存或以隐身模式加载网站/应用程序(适用于 Google Chrome 用户)。 在孩子身上

     {% extends "base.html" %}
     {% load static %}
     {% block title %}Rants{% endblock %} 
     {% block additionalStyles %}
         <link rel="stylesheet" href="{% static 'path_to_folder/rants.css' %}"/>
     {% endblock %}
    

在父级中

  {% load static %}
    <title>Parent site{% block title %}{% endblock %}  </title>
    <link rel="stylesheet" href="{% static 'path_to_folder/base.css' %}"/>
    {% block additionalStyles %} {% endblock %}

【讨论】:

    【解决方案2】:

    需要给&lt;link&gt;它自己的线路

    {% extends "base.html" %}
    {% load static %}
    {% block title %}Rants{% endblock %} 
    {% block additionalStyles %}
    <link rel="stylesheet" href="{% static 'rants.css' %}"/>
    {% endblock %}
    

    现在有效

    【讨论】:

      猜你喜欢
      • 2013-10-12
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 2020-08-11
      • 2015-06-09
      • 2012-03-09
      • 2016-02-26
      • 1970-01-01
      相关资源
      最近更新 更多