0

Django 1.10 using runserver in development

For some reason, no files can be found in my static directory unless I have it listed as such:

STATIC_URL = 'static/'                                   
if not os.environ.get('DEPLOYED'):                       
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")      

But all documentation and everything I have done before says it should be /static/ which makes me think I'm doing something wrong elsewhere. I am using Django's runserver

URLS:

if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles import views

    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

I get to the site now by typing localhost:8000/static/index.html

Diesel
  • 5,099
  • 7
  • 43
  • 81
  • 1
    You don't need to manually add static urls to your urlpatterns. This is done automatically: https://docs.djangoproject.com/en/1.10/howto/static-files/#configuring-static-files - "During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True" – Eugene Prikazchikov Oct 29 '16 at 02:24
  • I see this now. However if I remove the urlpattern line from urls.py and set STATIC_URL to /static/ or static/ it does not work. However it does work as displayed above. The error I get is 'index.html' could not be found using the same address – Diesel Oct 29 '16 at 02:30
  • 1
    it should be working. Try findstatic command: https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#findstatic with "--verbosity 2" to see which file pathes are actually searched. – Eugene Prikazchikov Oct 29 '16 at 02:32
  • Thank you, I had a basic misunderstanding. I'll write up my answer if anyone needs it. You helped me find that. – Diesel Oct 29 '16 at 05:40

1 Answers1

1

It was a simple misunderstanding. I did what Eugene recommended

findstatic index --verbosity=2

The output was 3 static folders that were not my static folder. I realized I had a basic misunderstanding of STATIC_ROOT and STATICFILES_DIRS. The path to my static folder was not in STATICFILES_DIRS in development.

I added the static folder to STATICFILES_DIRS and I was good. I removed the URL reference as suggested. I had to change STATIC_ROOT to not be that folder... which makes sense because a collectstatic will move everything to the STATIC_ROOT folder for deployment.

This post also helped

Community
  • 1
  • 1
Diesel
  • 5,099
  • 7
  • 43
  • 81