I just setup a simple Django website for development, and commented out
path('admin/', admin.site.urls)
,
with STATIC_URL = '/static/'
in the settings.py,
When I do python manage.py runserver 8100
and goto http://localhost:8100/static/admin/css/nav_sidebar.css
, I see
this static file is magically severed.
What’s really going on? I have not setup the static url serving in my urls.py yet? I also do not have another static server like NGINX.
3
Answers
So from https://docs.djangoproject.com/en/3.2/howto/static-files/, when you set
DEBUG=True
, Django will automatically do collectstatics and server the static file thur the URLs for you.I think the static files are served by e.g. NGINX. They are put to a location where they are accessible once you execute
python manage.py collectstatic
, and then Django does not care about them any more, because they are taken care off by your server. They are outsourced, so to say. Its a different service that handles them. So, the/static
will be remain accessible regardless of the admin URLs. What you switch on and off with the admin URLs are the pages under/admin
.Will not work anymore, but
will stay untouched.
When you do a
python manage.py runserver
you are using the django’s inbuilt development server which serves both static files and app logic. So it is an app server + static file server. If you use a specific app server like gunicorn, then you have to use an nginx to server static files and this is what you should do in production.