Franz Payer
Franz Payer

Reputation: 4137

django static files not working

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.

Upvotes: 16

Views: 64315

Answers (8)

Emmanuel Gyimah
Emmanuel Gyimah

Reputation: 11

Remember the name STATICFILES_DIRS not anything else it is case sensitive

Upvotes: 1

sruthisanoj
sruthisanoj

Reputation: 11

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

Upvotes: 0

Mayur Gupta
Mayur Gupta

Reputation: 519

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.

Upvotes: 0

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

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/

Upvotes: 18

deepak kumar sharma
deepak kumar sharma

Reputation: 99

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

Upvotes: 4

Amandeep Singh
Amandeep Singh

Reputation: 521

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

Upvotes: 0

Pratik Mandrekar
Pratik Mandrekar

Reputation: 9568

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

Upvotes: 8

Vaibhav Mishra
Vaibhav Mishra

Reputation: 12102

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

Upvotes: 5

Related Questions