I had tried to setup static files in Django but failed.
I have the following directory structure
app
|
|- manage.py
|- requirements.txt
|- static
| |
| |- css
| |
| |- snapweb.css
|
|
}- templates
|- web
|
|- __init__.py
|- settings.py
|- urls.py
|- wsgi.py
/app/web/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
import logging
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
]
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
/app/web/settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Take note that, I didn't run python manage.py collectstatic. As, if I want to run, I will get the following warning, which I have no idea how to solve it.
/app # python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
/app/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel:
So, my problem is, I feel confused on my app behavior.
The following URL works. I didn't expect it to work, as I don't see /app/static/admin/css/base.css file exist. Where does Django pick up the file?
https://localhost:2053/static/admin/css/base.css
I also confused, for the following URL not working. Even though file /app/static/css/snapweb.css is there. Why Django doesn't pick up the file?
https://localhost:2053/static/css/snapweb.css
I wish both URLs, https://localhost:2053/static/admin/css/base.css and https://localhost:2053/static/css/snapweb.css will work. Is there any setup I had missed?
Also, how I can run python manage.py collectstatic successfully, without overwriting my /app/static?
'django.contrib.admin'must be a registered inINSTALLED_APPS. It loads admin modules required.