Django cannot find static files. Need a second pai

2020-01-29 06:35发布

问题:

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.

回答1:

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



回答2:

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.css inside the dir named , while putting

{% 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.



回答3:

Change STATIC_ROOT of settings.py, I hope it will work. I face the same problem....

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/'