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

Question:

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.

Asked By: CowardlyFrench

||

Answers:

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

Answered By: argaen

Well, just going through the tutorial, I was stuck with this problem pretty much. Simply restarting the web-service fixed it.
So, if just completing the tutorial instructions, put a directory static/<your_application_name>/style.css inside the dir named <you_app_name>, 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.

Answered By: efi

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

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Answered By: Jahir islam

I have this issue with static files in Django
it take one day to find out what’s happen that static files not load and not showed in my project in runserver mode.

finally I found where the problem is…

I figure that this problem is because of permission issues in your file system.
you have to get permission to static files in your project and you see the problem is solved.

in linux you can run this command:

sudo chmod 777 <your static folder relative address> -R
Answered By: HaSSaN.Doodiyan

Change your TEMPLATE_DIRS according to it version

If your version is 3.1 or below

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

If it is above 3.2

STATICFILES_DIRS = [
BASE_DIR / "static",]

check this link for more clarity

python manage.py collectstatic

This command helped me alot after hours that that I was stuck

Answered By: shakiiir