django staticfiles at url root
You can manually add extra locations that do not exist within the static
directories within your project:
urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
)
if settings.DEBUG:
urlpatterns += static('/css/', document_root='app_root/path/to/css/')
urlpatterns += static('/images/', document_root='app_root/path/to/images/')
urlpatterns += static('/js/', document_root='app_root/path/to/js/')
This will map the media for the DEBUG dev server. And when you are running your production mode server, you will obviously be handling these static locations from the web server instead of sending the request through to django.
DJango not finding static files, despite STATIC_URL and STATIC_ROOT set
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS
, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns
.
Serving static files during development require add to urls.py
:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Unable to join static file to root directory in Django
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL
is the path on your website url.
STATIC_ROOT
is the path on your server where static files are located.
Serving static files from root of Django development server
note that by default Django won't serve a directory listing. Do you still get a 404 if file
/Users/kyle/Development/project/site/app/beer.jpg
doesn't appear ashttp://localhost/beer.jpg
?in
urls.py
URLs don't start with a slash; compareurl(r'beer')
withurl(r'^/beer')
I suggest just going for the standard STATIC support. It's awkward, but lets you serve file simply during development, and switch to a 3rd party server (ie Nginx) for production:
https://docs.djangoproject.com/en/dev/howto/static-files/
Django - serving static files from collect_static folder in dev
If you're running your django application on a server dev/staging etc, typically you'll have apache or nginx doing that and you can configure /static/
or what have you to serve your static files from the static collection path.
If you're using runserver
you're typically running in debug mode and django will [magically] serve static for you & load files from apps rather than collecting it all together in 1 place.
This is done with the following in your urls.py
from django.conf import settings
from django.contrib import admin
from django.urls import re_path, path, include
from django.views.generic import RedirectView, TemplateView
from django.views.static import serve
urlpatterns = [
...
]
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns += [
re_path(r'^static/(?P<path>.*)$', serve),
re_path(
r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}
),
]
You might also have django debug toolbar, or error templates that you'd like to be able to test, so you can add these to URLs for local testing like this;
if settings.DEBUG:
urlpatterns += [
path('400/', TemplateView.as_view(template_name='400.html')),
path('403/', TemplateView.as_view(template_name='403.html')),
path('404/', TemplateView.as_view(template_name='404.html')),
path('500/', TemplateView.as_view(template_name='500.html')),
path('502/', TemplateView.as_view(template_name='502.html')),
path('503/', TemplateView.as_view(template_name='503.html')),
re_path(r'^static/(?P<path>.*)$', serve),
re_path(
r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}
),
]
try:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
except ImportError:
pass
Static Root / Url not working ?
There is a difference between static root and staticfiles dirs
STATICFILES_DIRS
tells django where to look for the static files apart from the standard app directories.
STATIC_ROOT
tells where to collect all the static files on using the collectstatic
command.
You need to set STATIC_ROOT
to some other path from where your webserver (nginx) can server them directly in production.
Related Topics
Primefaces Datatable Selectcheckboxmenu Filter Formatting Issue
Float:Right Not Floating All The Way to The Right
Are CSS3 Properties Like Animate Too CPU Intensive
How to Change The Scrollbar to Custom Design? (Avoid Using The Default Browser Look)
How to Refresh Mat-Calendar After Changing The Background of Highlighted Dates
How to Revert Webkit-Appearance for Input[Type="Search"] of Normalize.CSS
HTML5 Video with Fixed Height, But Scale Width to 100%
Offering Smaller Versions of Videos with Media Query or When Mobiles Are Detected
Background Color Not Showing for a Div
Why Border of <Th> and <Thead> Both Not Showing Here
Is This How You Would Structure Your CSS Stylesheet
CSS Width/Height Pixel and Percentage Calculation Combintaion
Media Query to Target Most of Smartphone
Make CSS of Gradient from Image Style
Even Flexbox Product Grid with Left Alignment
How to Display a Bullet List Using Ionic Framework
Trying to Build Less (Less CSS) Using a Build Script with Nodejs
How to Pass Value from Ts File to SCSS File in Same Component in Angular 2+