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 fromSTATICFILES_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
-
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()
- How to setup SysLogHandler with Django 1.3 logging dictionary configuration
- Matplotlib svg as string and not a file
- Django collectstatic no such file or directory
- Django form with multiple file fields
- Testing for empty/null string in django
- How do I reply to an email using the Python imaplib and include the original message?
- Fields and base_fields β Django
- What is a good Django workflow?
- Auto-creating related objects on model creation in Django
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.
- open the
setting.py
file in your main project folder then - Paste this code inside
settings.py
at last:
STATIC_URL = '/static/'
# Added Manually
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Then restart VS Code.
- "Apps aren't loaded yet" when trying to run pytest-django
- Django β authentication, registration with email confirmation
0π
Here main directory name is mysite, and the app name is polls(polling_app project Django tutorial).
-
Create a static folder inside the main folder( mysite ).
-
We can add static files(such as JavaScript, imagefile, css etc) in static folder.
-
After creating static folder we have to inform Django on this by adding below line in settings.py(below the line STATIC_URL = β/static/β)
- 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
-
STATICFILES_DIRS =
[os.path.join(BASE_DIR, βmysite/static/β)
]
#Run this command in terminal to collect all static files in our project.
- python manage.py collectstatic
#Run the server, add port number if needed.
- python manage.py runserver portnumber
- How to display the message passed to Http404 in a custom 404 Error template in Django?
- Many to many relationships with additional data on the relationship