1

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?

1
  • 'django.contrib.admin' must be a registered in INSTALLED_APPS. It loads admin modules required. Commented Aug 12, 2018 at 19:01

1 Answer 1

1

First, you seem to be missing the django distinction between apps and the project (given you have named your project "app"). The project is what has manage.py and settings.py. The apps are what have models.py files.

Given you are using static files from the admin app as well as your own, you are going to need to do some restructuring. Django is looking for static files in STATIC_ROOT. In a "proper" setup, you're supposed to put static files in directories specific to your apps (as opposed to your project). The project static file (pointed to by STATIC_ROOT should have nothing you defined in it since that's where Django is going to put all your files when you run collectstatic.

Try moving your static folder to be a subdirectory of your app folder ("web/static") and running collectstatic.

See the docs for more info.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Do you have idea why https://localhost:2053/static/css/snapweb.css doesn't work, even though /app/static/css/snapweb.css is there?
I've changed my recommendation. Hope this helps
After moving /app/static folder to /app/web/static, and run collectstatic, I can see /app/static/admin/css is created. But, /app/static/web/css or /app/static/css is not created. Seem like, Django doesn't notice /app/web/static folder?
Check what's the value of STATICFILES_DIRS. And what is actually stored in STATIC_ROOT. You can print that out on the start of app.
Thanks. I'm able to make it work, by adding STATIC_ROOT = os.path.join(BASE_DIR, 'static') and STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'web/static'), ] in settings.py, and execute collectstatic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.