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.
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 eg "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]
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/
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}),
)
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
You can do such settings like
Inside settings.py add static directory path
STATICFILES_DIRS = [ BASE_DIR / "static" ]
Ref: https://docs.djangoproject.com/en/3.1/howto/static-files/
add a line of code in urls.py
urlpatterns += staticfiles_urlpatterns()
I am using the following setup: (in Apr 2021)
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.
setting.py
file in your main project folder thensettings.py
at last:STATIC_URL = '/static/'
# Added Manually
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Then restart VS Code.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.