Python Django 找不到静态文件。需要第二双眼睛,我快疯了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24199029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django cannot find static files. Need a second pair of eyes, I'm going crazy
提问by CowardlyFrench
Django will not serve my static files. Here's the errorreturned:
Django 不会提供我的静态文件。这是返回的错误:
[13/Jun/2014 06:12:09] "GET /refund/ HTTP/1.1" 200 2927
[13/Jun/2014 06:12:09] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 1667
[13/Jun/2014 06:12:09] "GET /static/css/cover.css HTTP/1.1" 404 1643
[13/Jun/2014 06:12:09] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 1661
[13/Jun/2014 06:12:09] "GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667
[13/Jun/2014 06:12:09] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 1661
[13/Jun/2014 06:12:09] "GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667
Here's the relevant part of my base.htmlfor bootstrap:
这是用于引导程序的base.html的相关部分:
!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">
<title>Cover Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
{
<link href="{% static "css/bootstrap.min.css"%}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{% static "css/cover.css" %}" rel="stylesheet">
Relevant code from settings.py(Note: The templates worked just fine.)
settings.py 中的相关代码(注意:模板工作得很好。)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATIC_URL = '/static/'
STATIC_ROOT = ''
And here's how my django project is laid out:
这是我的 Django 项目的布局方式:
- admin
- db.sqlite3
- project name
- manage.py
- app name
- static
- css
- ...
- dist
- ...
- css
- template
- base.html
- 行政
- 数据库.sqlite3
- 项目名
- 管理文件
- 应用名称
- 静止的
- css
- ...
- 区
- ...
- css
- 模板
- 基本文件
Any help at all would be greatly appreciated. I don't know what I could possibly have done wrong, I've looked at the code a million times and clearly it must be a gap in my understanding of how Django serves static files; however I have done this previously with no issues.
任何帮助都将不胜感激。我不知道我可能做错了什么,我已经看了一百万次代码,显然这一定是我对 Django 如何提供静态文件的理解有差距;但是我之前已经这样做了,没有任何问题。
采纳答案by argaen
do the following:
请执行下列操作:
If you are in DEBUG, set STATICFILES_DIRS = ("path/to/static") variable in your settings.py. It should then work onlyin DEBUG mode.
If you want it to also work in deploy mode, set STATIC_ROOT = ("path/to/static_root") variable in the settings.py. Then, execute
python manage.py collectstatic
and it should also work.
如果您处于调试状态,请在 settings.py 中设置 STATICFILES_DIRS = ("path/to/static") 变量。然后它应该只在调试模式下工作。
如果您希望它也能在部署模式下工作,请在 settings.py 中设置 STATIC_ROOT = ("path/to/static_root") 变量。然后,执行
python manage.py collectstatic
它也应该工作。
And now, for a better understanding how django manages static files:
现在,为了更好地理解 django 如何管理静态文件:
Django has some predefined places to look for static files and collect them, you specify where to find them with the STATICFILES_FINDERS in your settings.py. By default, it looks for static folder insideyour apps. You can tell Django to look for static files in other parts by setting the STATICFILES_DIRS variable (list or tuple of paths).
In DEBUG mode, staticfiles are picked from this paths (not from the static_root where you collect your files).
When you execute
python manage.py collectstatic
, Django goes through all directories where static files can be found, and places them in your static root. When you run in deploy mode, static files are served from this directory.
Django 有一些预定义的位置来查找和收集静态文件,您可以使用 settings.py 中的 STATICFILES_FINDERS 指定在哪里可以找到它们。默认情况下,它会在您的应用程序中查找静态文件夹。您可以通过设置 STATICFILES_DIRS 变量(路径列表或元组)告诉 Django 在其他部分查找静态文件。
在调试模式下,静态文件是从这个路径中挑选出来的(而不是从你收集文件的 static_root 中)。
当您执行 时
python manage.py collectstatic
,Django 会遍历所有可以找到静态文件的目录,并将它们放在您的静态根目录中。在部署模式下运行时,将从该目录提供静态文件。
PS: What I normally do is to create an app called common and create a static dir inside to place all common css, js for my project (and also for templates). This way, I don't have to specify the STATICFILES_DIRS variable.
PS:我通常做的是创建一个名为 common 的应用程序,并在其中创建一个静态目录,以便为我的项目(以及模板)放置所有常见的 css、js。这样,我不必指定 STATICFILES_DIRS 变量。
Hope it is clear now =).
希望现在清楚了=)。
回答by efi
Well, just going through the Tutorial, I was stuck with this problem pretty much. Simply restarting the web-service has fixed it. So, if just completing the Tutorial instructions, put a directory static//style.cssinside the dir named , while putting
好吧,只是通过教程,我几乎被这个问题困住了。只需重新启动 Web 服务即可修复它。因此,如果只是完成教程说明,请将目录static//style.css放入名为 的目录中,同时放入
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static '<your_app_name>/style.css' %}" />
inside head-section of a html-files (for ex., index.html) that should be designed with that css-file.
应使用该 css 文件设计的 html 文件(例如,index.html)的头部内部。
回答by Jahir islam
Change STATIC_ROOT of settings.py, I hope it will work. I face the same problem....
更改 settings.py 的 STATIC_ROOT,我希望它会起作用。我面临同样的问题......
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATIC_URL = '/static/'