[Fixed]-Django static files not working

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

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/

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}),
        )

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()
    

0πŸ‘

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

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.

πŸ‘€Mayur Gupta

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 portnumber
πŸ‘€sruthisanoj

0πŸ‘

Remember the name STATICFILES_DIRS not anything else it is case sensitive

Leave a comment