Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

python - Django cannot find static files. Need a second pair of eyes, I'm going crazy

Django will not serve my static files. Here's the error returned:

[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.html for bootstrap:

!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.)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

STATIC_URL = '/static/'

STATIC_ROOT = ''

And here's how my django project is laid out:

  • admin
  • db.sqlite3
  • project name
  • manage.py
  • app name
  • static
    • css
      • ...
    • dist
      • ...
  • template
    • base.html

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

do the following:

  1. If you are in DEBUG, set STATICFILES_DIRS = ("path/to/static") variable in your settings.py. It should then work only in DEBUG mode.

  2. 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.

And now, for a better understanding how django manages static files:

  • 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 inside your 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.

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.

Hope it is clear now =).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...