2

I've run into a roadblock here and the Django docs and previous stack overflow questions aren't helping. I've read over the static files section in the docs and I seem to be doing the right thing with django.contrib.staticfiles.

So I'm trying to display my static files, and I'm a little bit baffled to what I'm doing wrong. When I go to my localhost:8000/static/ site, I get a 404 error with a "Directory indexes are not allowed here." message. I've set my STATIC_URL' = '/static/', I have a static directory with my files located inside of my my newsletter app which is present under the INSTALLED_APPS setting. I've even tried putting the full path in the STATICFILES_DIR setting and that doesn't work.

I've seen people try to modify the STATIC_ROOT setting but, correct me if I'm wrong, I thought this is bad practice on a dev server. Any leads? Thank you.

Community
  • 1
  • 1
Alex Roe
  • 586
  • 5
  • 11

3 Answers3

4

It's saying it won't generate an HTML document containing a list of the files in that directory (which it is referring to as a "directory index"). You should still be able to access the files directly. Try loading a specific file instead of just putting the bare directory name into your browser.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • Yes - When I go to `...../my_project/my_app/static/` I can see my files just fine. So this should still work when I'm rendering my templates? Because in my index.html file, I'm linking to my css document and my link is `src = {{ STATIC_URL}}style.css` and when I examine the get request, it is requesting just straight up style.css and therefore not returning my css file. I'm a django noob..sorry hah – Alex Roe Jul 29 '12 at 02:55
2

Hate to answer my own question, but a buddy with a lot more django experience than me pointed me in the right direction. I moved my static folder into the outer directory where manage.py is. Changed my STATIC_URL to /static, then added urlpatterns += staticfiles_urlpatterns() to my urls.py file. Now it all works!

I am still a bit confused because I thought with 1.4 I would be able to add the static directory under my app and have it work just fine... anyhow thanks for all the responses!

Alex Roe
  • 586
  • 5
  • 11
  • That is strange. Did you manually move the files to the outer directory? You should try using the 'python manage.py collectstatic' command to do that https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic – Intenex Jul 30 '12 at 00:22
  • Yes - I manually moved them. Thanks! I'll have to try that next time. – Alex Roe Jul 30 '12 at 13:51
  • 1
    strange that you set STATIC_URL = /static and it worked because with django-1.8 it complains if STATIC_URL does not end with / :django.core.exceptions.ImproperlyConfigured: If set, STATIC_URL must end with a slash – Divick Nov 18 '15 at 08:20
0

As per your comment on Jim's answer, it looks like Django's just not getting a proper value for {{ STATIC_URL }}.

Looking at your code, it looks like in the settings you have

STATIC_URL' = '/static/'

with a single quote mark after the STATIC_URL where there shouldn't be one. Try

STATIC_URL = '/static/'

Does that solve your problem?

Intenex
  • 1,907
  • 3
  • 20
  • 33
  • Ahh, just noticed you didn't actually visit the file in your browser, you just visited the location on your hard drive lol. What Jim meant is try visiting localhost:8000/static/style.css if that's where your css file is (not on your computer /my_project/my_app/static/), and see if it loads. If it loads correctly, everything's working fine and you should be able to access your static files from your templates. – Intenex Jul 29 '12 at 08:06