简体   繁体   中英

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?

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM