【问题标题】:Adding style tag in django inherited template在 django 继承的模板中添加样式标签
【发布时间】:2018-12-18 18:12:29
【问题描述】:

我是 Django 新手。我想在我的 Django 模板中添加几行样式代码,即“blogs.html”,它已经从“base.html”django 模板继承。但是如果我添加一个样式标签并尝试使用{%static %} 它不起作用。

博客.html

{% extends 'base.html'%}

{% load staticfiles %}

{% block content %}
    <style>
        body{
               margin:0;
               padding:0;
        }

        box1{
               height:100vh;
               width:100%;
               background image:{%static 'images/ps1.jpg'%};
               background-size:cover;
        }
        box2{
               height:100vh;
               width:100%;
               background image:{%static 'images/ps2.jpg'%};
               background-size:cover;
        }
        box3{
               height:100vh;
               width:100%;
               background image:{%static 'images/ps3.jpg'%};
               background-size:cover;
        }
    </style>

    <div id="box1">
    </div>
    <div id="box2">
    </div>
    <div id="box3">
    </div>
{% endblock %}

【问题讨论】:

    标签: python django html css django-templates


    【解决方案1】:

    我假设你的 {% block content %} {% endblock %} 在 html body 中,但 CSS 代码应该在 head 中。这就是它不起作用的原因。

    如果你真的需要这个,那么你可以在 base.html head 部分创建一个块。 像这样的

    <head>
    {% block mycss %} {% endblock mycss %}
    </head>
    

    和in继承的html

    {% extends 'base.html'%}
    
    {% load staticfiles %}
    {% block mycss %} <style> Your entire css </style> {% endblock %}
    
    {% block content %}
        Body Part
    {% endblock %
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-26
      • 2012-10-07
      • 2015-11-13
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多