Django:我如何使用STATICFILES_FINDERS

时间:2016-06-07 12:15:11

标签: python django

STATICFILES_FINDERS中给出的默认设置似乎是用于查看应用内的静态文件夹。我试图让Django在顶级静态内搜索(与manage.py相同的水平),但是尽管在STATIC_ROOT中定义了settings.py目录,它似乎也没有这样做做collectstatic。在查找jQuery文件时出现404错误。

如何更改此设置,以便Django查看我的顶级静态文件夹?

以下是运行我的网站时出现的jQuery 404错误:

Django version 1.9.6, using settings 'tictactoe.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[06/Jun/2016 23:04:21] "GET /board/ HTTP/1.1" 200 361
[06/Jun/2016 23:04:21] "GET /static/jquery-2.2.4.js HTTP/1.1" 404 1658
[06/Jun/2016 23:04:21] "GET /static/css/boardcss.css HTTP/1.1" 304 0

base.html文件:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="{% static 'jquery-2.2.4.js' %}">
</script> 
<link rel="stylesheet" href="{% static 'css/boardcss.css' %}">
</head>
<body class="body" style="background-color:#545454">
    <div class='container-fluid'>
               {% block content %}
               {% endblock %}   
    </div>
</body>

</html>

settings.py:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

]

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "board", "static"),
]

STATIC_ROOT = os.path.join(BASE_DIR, "static")

我的目录树:

adsf

1 个答案:

答案 0 :(得分:2)

STATIC_ROOT旨在与collectstatic命令read docs配合使用。如果您希望发现文件夹,则需要将其添加到STATICFILES_DIRS

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "board", "static"),
    os.path.join(BASE_DIR, "static"),
]

<强>更新

在评论中回答您的问题

static文件夹重命名为staticfilesSTATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

我更喜欢第一个。但是您需要在STATICFILES_DIRS中添加os.path.join(BASE_DIR, "staticfiles")而不是os.path.join(BASE_DIR, "static")

喜欢这个

STATICFILES_DIRS = [[
    os.path.join(BASE_DIR, "board", "static"),
    os.path.join(BASE_DIR, "staticfiles"),
]