I am trying to make Django's development server to serve static files that have been collected by the python manage.py collectstatic
command. For now I failed.
My Django settings file declares this:
STATIC_ROOT = os.path.join(WWW_PATH, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(ROOT_PATH, 'front', 'public'),
os.path.join(ROOT_PATH, 'front', 'dist')
)
This makes the collectstatic
command copying files from os.path.join(ROOT_PATH, 'front', 'public')
and os.path.join(ROOT_PATH, 'front', 'dist')
to STATIC_ROOT
, and it works perfectly.
I was assuming that it would also tell Django to look for static files into the STATIC_ROOT
directory, I was wrong. Even if the STATIC_ROOT
directory does not exists, Django is able to serve the static files. But if the os.path.join(ROOT_PATH, 'front')
is missing, Django no more serve the static files.
This shows that Django continues to serve static files from the sources directories and not from STATIC_ROOT
.
So I would like to know if there is a way to instruct Django's development server to serve static files from STATIC_ROOT
. Any hint?
EDIT:
After @e4c5 's answer I modified my root urls.py
like this:
static_patterns = [
url(r'^$', TemplateView.as_view(template_name='index.html'))
]
urlpatterns = [
url(r'^', include(static_patterns)),
url(r'^admin/', admin.site.urls),
url(r'^api/resa/', include('reservation.urls')),
url(r'^api/auth/', include('authentication.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
unfortunately, it does not have the expected result and Django does not find static files in STATIC_ROOT
, I doubt that it actually looks for static files in STATIC_ROOT
.
here is what I can see in the console when accessing to the index page:
[04/Jun/2017 16:18:05] "GET / HTTP/1.1" 200 1411
[04/Jun/2017 16:18:05] "GET /static/style/index.css HTTP/1.1" 404 1759
[04/Jun/2017 16:18:05] "GET /static/style/react-datetime.css HTTP/1.1" 404 1786
[04/Jun/2017 16:18:05] "GET /static/style/react-big-calendar.css HTTP/1.1" 404 1798
[04/Jun/2017 16:18:05] "GET /static/script/bundle.js HTTP/1.1" 404 1762
here are the content of the settings variables:
>>> from django.conf import settings
>>> settings.WWW_PATH
'/home/tryph/PycharmProjects/resa/www'
>>> settings.STATIC_ROOT
'/home/tryph/PycharmProjects/resa/www/static'
>>> settings.STATIC_URL
'/static/'
here is the content of the WWW_PATH
directory:
/home/tryph/PycharmProjects/resa/www
└── static
├── admin
│ [...]
├── favicon.ico
├── index.html
├── rest_framework
│ [...]
├── script
│ └── bundle.js
└── style
├── index.css
├── react-big-calendar.css
└── react-datetime.css
This is done using static.serve
There may be files other than your project’s static assets that, for convenience, you’d like to have Django serve for you in local development. The serve() view can be used to serve any directory you give it. (This view is not hardened for production use and should be used only as a development aid; you should serve these files in production using a real front-end web server
Change your urls.py like this:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With