Django cannot find static files. I need a second pair of eyes, I'm losing my mind

Django will not serve my static files. This returns an error :

[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 is 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 fine.)

 TEMPLATE_DIRS = ( os.path.join(BASE_DIR, 'templates'), ) STATIC_URL = '/static/' STATIC_ROOT = '' 

And here is how my django project works out:

  • admin
  • db.sqlite3
  • Project name
  • manage.py
  • app name
  • Static
    • CSS
      • ...
    • distance
      • ...
  • template
    • base.html

Any help at all would be greatly appreciated. I don’t know what I may have done wrong, I looked through the code a million times and obviously this should be a gap in my understanding of how Django serves static files; however, I did this earlier without any problems.

+16
python django static twitter-bootstrap web
source share
3 answers

follow these steps:

  • If you are on DEBUG, set settings.py to STATICFILES_DIRS = ("path / in / static"). Then it should work only in DEBUG mode.

  • If you want it to also work in deployment mode, set the STATIC_ROOT = ("path / in / static_root") parameter to settings.py. Then do python manage.py collectstatic and it should work as well.

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

  • Django has certain predefined places to search for static files and collect them, you specify where to find them using STATICFILES_FINDERS in your settings.py file. By default, it searches for static folders inside your applications. 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, static files are selected from these paths (not from static_root where you collect the files).

  • When you execute python manage.py collectstatic , Django scans all directories in which static files can be found, and places them in your static root. When launched in deployment mode, you can load static files from this directory.

PS: I usually create an application called shared and create a static directory inside to host all the common css, js for my project (as well as for templates). Thus, I do not need to specify the STATICFILES_DIRS variable.

Hope it’s clear now =).

+34
source share

Well, just going through the tutorial, I am very stuck in this problem. Fixed just restarting the web service. So, if you just complete the Tutorial instructions, put the static // style.css directory in the dir directory, placing

 {% load staticfiles %} <link rel="stylesheet" type="text/css" href="{% static '<your_app_name>/style.css' %}" /> 

inside the head-section html files (e.g. index.html ) that should be constructed with this css file.

+4
source share

Change STATIC_ROOT to settings.py, I hope this works. I am facing 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/' 
0
source share

All Articles