Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 and the ever confusing "Static Files"

I've fired up a new Django 1.8 project and am hitting the wall of trying to serve static files in the development environment. I'm pretty sure I've followed the docs explicitly, but it's just not working.

Settings.py

STATICFILES_DIR = (                                                                 
  os.path.join(BASE_DIR, 'static/'),                                                
  BASE_DIR                                                                          
)                                                                                                                                                                 

STATIC_URL = '/static/'                                                             
STATIC_ROOT = '/srv/www/cpm2/static/'    

urls.py:

from django.conf.urls import include, patterns, url 
from django.conf import settings
from django.conf.urls.static import static·
from django.contrib import admin
from django.views.generic import TemplateView

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)),
    url(r'^test/', TemplateView.as_view(template_name="basics/base.html")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

templates/basics/base.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">


    {% load staticfiles %}
    <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">

  </head>


  <body>
      <h1>Hello, world!</h1>

  </body>
</html>

When I go to http://my_server_ip:8000/test/ none of the CSS is loaded. Viewing the page source, I see this where that CSS link is <link href="/static/css/bootstrap.css" rel="stylesheet"> which is good, but when I try to follow that link directly, it fails giving me a 404 error.

Now, I've set up my Apache server to serve the files directly, they work. So, going to http://my_server_ip/static/css/bootstrap.css works - so I know the files are readable. It's just they don't work in development.

What am I doing wrong? As so many others before me, I'm pulling my hair out!

EDIT Trying to access the file directly gives me this:

Page not found (404)
Request Method: GET
Request URL:    http://my_server_ip:8000/static/css/bootstrap.css
Raised by:  django.views.static.serve
'css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

It doesn't even show a list of places its tried, just that the file isn't found.

like image 801
Garfonzo Avatar asked May 15 '15 15:05

Garfonzo


1 Answers

You are missing an 's' from STATICFILES_DIRS.

Also, you shouldn't be including your BASE_DIR in that setting - that could end up serving all your python code, which would be a bad idea.

like image 130
Alasdair Avatar answered Oct 06 '22 17:10

Alasdair