Talha Anwar
Talha Anwar

Reputation: 2949

In Django, which directory should static files be?

I have my static files in sub_app directory. I will name the folder sub_app where view.py, model.py are located. And when I run python manage.py findstatic it returns sub_app\static folder. I have another folder name as main_app, where settings.py file is located. These two folders and manage.py file is located in a root folder. I have no issue when DEBUG=True, but when I run DEBUG=False, I got following warning

venv\lib\site-packages\whitenoise\base.py:115: UserWarning: No directory at: root\static\
  warnings.warn(u"No directory at: {}".format(root))

Here are static files settings

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Where should my static files?

Upvotes: 1

Views: 1498

Answers (2)

Sukhamjot Singh
Sukhamjot Singh

Reputation: 35

Create a directory named static inside the app directory where the "view.py, model.py are located." Inside static you need to create another directory with the same name as of the app. Inside that folder you can create your static files which include anything like your css files

Just add this line in the settings.py of your app

STATIC_URL = '/static/'

As you mentioned sub_app you should create sub_app/static/sub_app directory.

Upvotes: 0

SevvyP
SevvyP

Reputation: 385

You can put your static files anywhere, as long as you let your application know where they are. You have it configured that

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

but you say that you have your static files in sub_app\static. The error is telling you that it's checking the root folder. You need to change your configuration to

STATIC_URL = '/sub_app/static/'
STATIC_ROOT = '/sub_app/static'

Upvotes: 2

Related Questions