15

For some reason, django is not serving up my static files.

I have already looked at a bunch of fixes for this problem, but I still have not found a solution.

Here is my configuration:

urls.py

urlpatterns = patterns('',
    (r'^$', index),
    (r'^ajax/$', ajax),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}),
)

settings.py

STATIC_ROOT = '/home/aurora/Code/django/test/static/'
STATIC_URL = '/static/'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

When I navigate to http://localhost:8000/static/css/default.css
I get this error: 'css/default.css' could not be found

When I navigate to http://localhost:8000/static/
I get this error: Directory indexes are not allowed here.

It looks like the static directory is getting mapped out, but the sub-directories are not.

Franz Payer
  • 4,069
  • 15
  • 53
  • 77
  • 1
    Please see my answer here: http://stackoverflow.com/questions/9168187/django-cannot-find-my-media-files-on-development-server/9168404#9168404 . Long and short, you don't store your static files in `STATIC_ROOT`. Django *never* uses this directory in development. Even in production, it's your webserver not Django that serves out of this directory. – Chris Pratt Aug 20 '12 at 21:52
  • Worked right away. post as an answer if you want me to mark as answer. – Franz Payer Aug 21 '12 at 13:19

7 Answers7

18

In development:

  • STATICFILES_DIRS should have all static directories inside which all static files are resident

  • STATIC_URL should be /static/ if your files are in the local machine otherwise put the base URL here e.g. "http://example.com/"

  • INSTALLED_APPS should include 'django.contrib.staticfiles'

In the template, load the staticfiles module:

{% load staticfiles %}
..
..
<img src='{% static "images/test.png" %}' alt='img' />

In Production:

  • Add STATIC_ROOT that is used by Django to collect all static files from STATICFILES_DIRS to it

  • Collect static files

python manage.py collectstatic [--noinput]
  • add the path to urls.py
from . import settings
    ..
    ..
urlpatterns = patterns('',
..
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)`

More detailed articles are listed below:

http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated

http://agiliq.com/blog/2013/03/serving-static-files-in-django/

fboaventura
  • 173
  • 3
  • 7
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 1
    Thank you. This summarizes the changes for dev and production quite well . In my case i had typed STATICFILES_DIR instead of STATICFILES_DIRS and had been struggling to get it to work – rks Aug 27 '18 at 17:05
7

Try running python manage.py collectstatic and see where the static files are being collected.

Add this to your urls.py and set DEBUG=True in settings.py

if settings.DEBUG:
    urlpatterns += patterns('',
             (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
         )

    urlpatterns += patterns('',
            (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
        )
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • 2
    0 static files copied to '/home/aurora/Code/django/test/static/'. I did an ls on the directory and I can see the files there... – Franz Payer Aug 20 '12 at 17:50
  • 'show_indexes':True should help you show list the indexes on `/static`. What do you see there? – Pratik Mandrekar Aug 20 '12 at 17:59
  • The directory gets listed when I go to /static/, but I cannot access files in the directory – Franz Payer Aug 20 '12 at 17:59
  • Does `localhost:8000/admin/` have all the `css` files? – Pratik Mandrekar Aug 20 '12 at 18:03
  • Try editing your settings.py to below static section and copy your css under the `project_static` folder and then run `the `collectstatic` command – Pratik Mandrekar Aug 20 '12 at 18:09
  • List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) #From the django.contrib.staticfiles package, to store all static application media (Non -user generated) STATIC_URL = "/static/" STATIC_ROOT = os.path.join(DIRNAME ,'static') STATICFILES_DIRS = ( os.path.join(DIRNAME , 'project_static'), ) – Pratik Mandrekar Aug 20 '12 at 18:11
5

I don't think you need your static path in urls.py, remove that and it should work.

currently it is like this

urlpatterns = patterns('',
    (r'^$', index),
    (r'^ajax/$', ajax),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}),
)

just remove the r'^static line

urlpatterns = patterns('',
    (r'^$', index),
    (r'^ajax/$', ajax),
)

at least this is how it is done in django 1.3 and up

Vaibhav Mishra
  • 11,384
  • 12
  • 45
  • 58
4

You can do such settings like

  1. Inside settings.py add static directory path

     STATICFILES_DIRS = [
         BASE_DIR / "static"
     ]
    

    Ref: https://docs.djangoproject.com/en/3.1/howto/static-files/

  2. add a line of code in urls.py

     urlpatterns += staticfiles_urlpatterns()
    
4b0
  • 21,981
  • 30
  • 95
  • 142
0
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Amandeep Singh
  • 503
  • 3
  • 5
0

I am using the following setup: (in Apr 2021)

  • macos big sure
  • vscode
  • Ananconda 3 (for environment)

When I am trying to access my static files in Django using http://localhost:8000/static/test.txt if inside static folder a test.txt file exists.

  1. open the setting.py file in your main project folder then
  2. Paste this code inside settings.py at last:
STATIC_URL = '/static/'

# Added Manually
STATICFILES_DIRS = [
    BASE_DIR / "static",
]

Then restart VS Code.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mayur Gupta
  • 303
  • 2
  • 14
0

Here main directory name is mysite, and the app name is polls(polling_app project Django tutorial).

  1. Create a static folder inside the main folder( mysite ).

  2. We can add static files(such as JavaScript, imagefile, css etc) in static folder.

  3. After creating static folder we have to inform Django on this by adding below line in settings.py(below the line STATIC_URL = ‘/static/’)

  1. STATIC_ROOT = os.path.join(BASE_DIR, 'static')

#Now tell django to look where for the static files you have added. #on top of settings.py file add import os

  1. STATICFILES_DIRS = [

    os.path.join(BASE_DIR, 'mysite/static/')

]

#Run this command in terminal to collect all static files in our project.

  1. python manage.py collectstatic

#Run the server, add port number if needed.

  1. python manage.py runserver portnummer
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 05 '23 at 14:19