Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 721
CowardlyFrench Avatar asked Jun 13 '14 06:06

CowardlyFrench


People also ask

How does Django find static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

Where should I put static files in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

What are static files Django?

django. contrib. staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production. For an introduction to the static files app and some usage examples, see How to manage static files (e.g. images, JavaScript, CSS).


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

like image 89
argaen Avatar answered Oct 19 '22 07:10

argaen