【问题标题】:django returns 403 Forbidden on background-imagedjango 在背景图像上返回 403 Forbidden
【发布时间】:2018-01-14 21:18:21
【问题描述】:

我目前从我的一个基本模板加载的样式表中收到 403 禁止错误。路径似乎正确,但服务器不会加载它。

模板base.html

<!DOCTYPE html>
{% load staticfiles %}
{% load static %}
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- BOOTSTRAP -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- GOOGLE FONTS -->
    <link href="https://fonts.googleapis.com/css?family=Lato|Open+Sans|Oswald|Raleway|Roboto+Condensed|Roboto:300,400,900" rel="stylesheet">
    <!-- NAV BAR CSS -->
    <link rel="stylesheet" href="{% static 'static/css/nav_style.css' %}"> <!-- Resource style -->
    <script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <!-- Resource jQuery -->
    <script src="{% static 'static/js/modernizr.js' %}"></script> <!-- Modernizr -->    
    <script src="{% static 'static/js/nav_main.js' %}"></script> <!-- Resource Nav -->
  </head>
  <body>
    <div class="container">
        {% block body_block %}
        {% endblock %}
    </div>
  </body>
</html>

我的 CSS 看起来像: nav_style.css

.cd-stretchy-nav.edit-content ul a::after {
  right: 7px;
  background-image: url(../img/stretchy_nav_cd-sprite-3.svg);
}

返回以下错误:

Failed to load resource: the server responded with a status of 403 (Forbidden)

项目文件夹具有以下结构:

myproject
-myapp
-- ...
-myproject
-- settings.py
-- ...

具体设置适用于为我所有资源提供服务的另一个静态应用程序

所以对于 settings.py 我有一个 BASE_DIR 和一个 REMOTE_DIR

TEMPLATE_DIR = os.path.join(REMOTE_DIR,"static/html")
STATIC_ROOT = REMOTE_DIR
STATIC_DIR = os.path.join(STATIC_ROOT,"static")

STATICFILES_DIRS = [
STATIC_DIR,
...,
]

STATIC_URL = '/static/'

静态应用程序作为我的 REMOTE_DIR 如下所示:

static_app
--admin
---css
---fonts
---img
---js
--static
---css
----nav_style.css
---fonts
---html
----myapp
-----base.html
---img
----stretchy_nav_cd-sprite-3.svg
---js
--...

【问题讨论】:

  • 能否添加项目文件和文件夹的快照?
  • @JuanMiGabarron ,我在上面的原始帖子中添加了这些信息。
  • 嗯,很奇怪,也许你应该在背景图片的 url 路径中添加“”:url("../img/stretchy_nav_cd-sprite-3.svg")
  • 它不会改变任何东西,看起来像一个安全功能。但我不确定如何在 django 中超越它...

标签: css django django-templates django-staticfiles


【解决方案1】:

目前还没有找到正确和简单的解决方案,最直接的解决方案是将样式表请求的资产(例如背景图像)直接移动到与 css 文件相同的文件夹中,并相应地调整 url。

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    相关资源
    最近更新 更多