5

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.

Garfonzo
  • 3,876
  • 6
  • 43
  • 78
  • Try adding {% load static %} at the top of your HTML page. You've loaded the "staticfiles" template tag but you're currently using the "static" template tag. – Hevlastka May 15 '15 at 15:54
  • Where are stored your staticfiles ? – Charlesthk May 15 '15 at 15:56
  • @Hevlastka Nope, same result (doesn't work) – Garfonzo May 15 '15 at 15:56
  • Have you ran python manage.py collectstatic ? – Hevlastka May 15 '15 at 15:59
  • @Charlesthk Static files are stored in `/srv/www/cpm2/static'` but, due to frustration, I also was storing them at `/srv/www/cpm2/cpm2/static/` to see if that worked too - neither work. (The project name is `cpm2`) – Garfonzo May 15 '15 at 16:02
  • @Hevlastka Yes - didn't help :( – Garfonzo May 15 '15 at 16:02
  • Could you edit the question and provide us with the feedback when you're trying to access the file directly? That traceback should list the folders of where the system is looking for the files. – Hevlastka May 15 '15 at 16:04
  • The best practice is to store your staticfiles in a subfolder of your app called "your_app/static/your_app" – Charlesthk May 15 '15 at 16:05
  • @Charlesthk Right, but, as the docs suggest, if you also have static files common to all apps, you should store those in a base `static` directory so log as you add the directory to `STATICFILES_DIR`, which I've done. – Garfonzo May 15 '15 at 16:11
  • Have you tried to explicitly set the path like STATICFILES_DIRS = ('/srv/www/cpm2/static',)? – Jingo May 15 '15 at 16:11
  • @Jingo Yes, still didn't work. – Garfonzo May 15 '15 at 16:11

1 Answers1

9

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 1
    And you win. That was my problem - a simple *spelling mistake*! And you're right, once I added the `S` the dev server was telling me to remove the `BASE_DIR` which I did, and now it all works. – Garfonzo May 15 '15 at 16:16